<!DOCTYPE html>
<html class=" mdzr-js mdzr-borderradius mdzr-boxshadow mdzr-cssanimations mdzr-svg" style="height: 100%;" lang="zh-hans"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="ROBOTS" content="ALL">
    <meta name="MSSmartTagsPreventParsing" content="true">
    <meta name="Copyright" content="Django Software Foundation">
    <meta name="keywords" content="Python, Django, framework, open-source">
    <meta name="description" content="">

    
  
    
      
    
  
  <link rel="canonical" href="https://docs.djangoproject.com/zh-hans/4.0/ref/templates/builtins/">
  
    
      
    
    <link rel="alternate" hreflang="el" href="https://docs.djangoproject.com/el/3.2/ref/templates/builtins/">
  
    
      
    
    <link rel="alternate" hreflang="en" href="https://docs.djangoproject.com/en/3.2/ref/templates/builtins/">
  
    
      
    
    <link rel="alternate" hreflang="es" href="https://docs.djangoproject.com/es/3.2/ref/templates/builtins/">
  
    
      
    
    <link rel="alternate" hreflang="fr" href="https://docs.djangoproject.com/fr/3.2/ref/templates/builtins/">
  
    
      
    
    <link rel="alternate" hreflang="id" href="https://docs.djangoproject.com/id/3.2/ref/templates/builtins/">
  
    
      
    
    <link rel="alternate" hreflang="it" href="https://docs.djangoproject.com/it/3.2/ref/templates/builtins/">
  
    
      
    
    <link rel="alternate" hreflang="ja" href="https://docs.djangoproject.com/ja/3.2/ref/templates/builtins/">
  
    
      
    
    <link rel="alternate" hreflang="ko" href="https://docs.djangoproject.com/ko/3.2/ref/templates/builtins/">
  
    
      
    
    <link rel="alternate" hreflang="pl" href="https://docs.djangoproject.com/pl/3.2/ref/templates/builtins/">
  
    
      
    
    <link rel="alternate" hreflang="pt-br" href="https://docs.djangoproject.com/pt-br/3.2/ref/templates/builtins/">
  
    
      
    
    <link rel="alternate" hreflang="zh-hans" href="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/a.htm">
  

  <link rel="search" type="application/opensearchdescription+xml" href="https://docs.djangoproject.com/zh-hans/3.2/search/description/" title="Django 文档">

    <!-- Favicons -->
    <link rel="apple-touch-icon" href="https://static.djangoproject.com/img/icon-touch.e4872c4da341.png">
    <link rel="icon" sizes="192x192" href="https://static.djangoproject.com/img/icon-touch.e4872c4da341.png">
    <link rel="shortcut icon" href="https://static.djangoproject.com/img/favicon.6dbf28c0650e.ico">
    <meta name="msapplication-TileColor" content="#113228">
    <meta name="msapplication-TileImage" content="https://static.djangoproject.com/img/icon-tile.b01ac0ef9f67.png">

    <title>内置模板标签和过滤器 | Django 文档 | Django</title>

    <link rel="stylesheet" href="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/output.css">
    <script src="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/webfontloader.js"></script>
    <link rel="stylesheet" href="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/css.css"><script>
    WebFont.load({
      custom: {
        families: ['FontAwesome', 'Fira+Mono'],
      },
      google: {
        families: ['Roboto:400italic,700italic,300,700,400:latin'
        ]
      },
      classes: false,
      events: false,
      timeout: 1000
    });
    </script>
    <script src="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/modernizr.js"></script>
    
  <script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="main.2f2c378dba28" src="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/main.js"></script><script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="mod/mobile-menu" src="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/mobile-menu.js"></script><script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="mod/version-switcher" src="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/version-switcher.js"></script><script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="mod/search-key" src="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/search-key.js"></script><script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="jquery" src="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/jquery.js"></script><style id="b4fea98d-cb66-4c0e-8bbc-e207bff7f8b9">
      html,body{
        top: 0!important;
      }
      #google_translate_element {
        position: fixed;
        bottom: 30px;
        height: 21px;
        border-radius: 11px;
        z-index: 10000000;
        overflow: hidden;
        box-shadow: 1px 1px 3px 0 #888;
        opacity: .5;
        transition: all .3s;
      }
      #google_translate_element .goog-te-gadget-simple {
        border: 0;
      }
      #google_translate_element .goog-te-gadget-simple span {
        margin-right: 0;
        border-radius: 11px;
      }
      #lb {
        display: inline-block;
      }
      .recoverPage {
        width: 4em;
        background-color: #fff;
        position: fixed;
        z-index: 10000000;
        bottom: 60px;
        user-select: none;
        text-align: center;
        font-size: small;
        line-height: 2em;
        border-radius: 1em;
        box-shadow: 1px 1px 3px 0 #888;
        opacity: .5;
        transition: all .3s;
      }
      #google_translate_element:hover, .recoverPage:hover {
        opacity: 1;
        transform: translateX(0);
      }
      .recoverPage:active {
        box-shadow: 1px 1px 3px 0 #888 inset;
      }
      #google_translate_element .goog-te-gadget-simple {
        width: 100%;
      }
      .goog-te-banner-frame.skiptranslate {
        display: none
      }
      #goog-gt-tt {
        visibility: hidden!important;
        display: none!important;
      }
      .goog-text-highlight {
        background-color: inherit!important;
        box-shadow: 0 0 0 0 transparent!important;
      }
      @media handheld, only screen and (max-width: 768px) {
        #google_translate_element {
          width: 104px;
        }
        #google_translate_element .goog-te-combo {
          margin: 0;
          padding-top: 2px;
          border: none;
        }
        .recoverPage {
          width: 1.5em;
          line-height: 1.5em;
        }
      }
    </style><style id="89f2fb95-d593-495e-bf42-5b2211bb6de5">
      #google_translate_element {
        right: 0;
        transform: translateX(85%);
      }
      .recoverPage {
        right: 0;
        transform: translateX(73%);
      }
      @media handheld, only screen and (max-width: 768px) {
        .recoverPage {
          transform: translateX(0);
        }
      }
    </style><script>function googleTranslateElementInit() {
        let google_translate_element = document.getElementById('google_translate_element')
        let timer = setInterval(function () {
          google_translate_element = document.getElementById('google_translate_element')
          if (google_translate_element) {
            clearInterval(timer)
            new google.translate.TranslateElement(
              {
                pageLanguage: 'auto',
                //包括的语言，中文简体，中文繁体，英语，日语，俄语
                includedLanguages: 'zh-CN,zh-TW,en,ja,ru',
                /*0，原生select，并且谷歌logo显示在按钮下方。
                 1，原生select，并且谷歌logo显示在右侧。
                 2，完全展开语言列表，适合pc。
               */
                layout: /mobile/i.test(navigator.userAgent) ? 0 : 2,
              },
              'google_translate_element'
            )
            // 清除图片的请求，加快访问速度
            let img = [].slice.call(document.querySelectorAll('#goog-gt-tt img,#google_translate_element img'));
            img.forEach(function(v) {
              const a = v
              a.src = ''
              let b = a.outerHTML.replace(/<img(.*?)>/, () => {
                return '<span id="lb"' + RegExp.$1 +'></span>'
              })
              const c = document.createElement('div')
              c.innerHTML = b
              a.parentNode.insertBefore(c.children[0], a.parentNode.children[0])
              a.remove()
            });
            const recoverPage = document.createElement('div')
            recoverPage.setAttribute('class', 'notranslate recoverPage')
            recoverPage.innerText = '原'
            document.body.appendChild(recoverPage)
            // 点击恢复原网页
            recoverPage.onclick = () => {
              const phoneRecoverIframe = document.getElementById(':1.container') // 移动端
              const PCRecoverIframe = document.getElementById(':2.container') // PC端
              if (phoneRecoverIframe) {
                const recoverDocument = phoneRecoverIframe.contentWindow.document
                recoverDocument.getElementById(':1.restore').click()
              } else if (PCRecoverIframe) {
                const recoverDocument = PCRecoverIframe.contentWindow.document
                recoverDocument.getElementById(':2.restore').click()
              }
            }
          }
        }, 300)
      }</script><script src="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/element.js"></script><link type="text/css" rel="stylesheet" charset="UTF-8" href="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/translateelement.css"><script type="text/javascript" charset="UTF-8" src="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/mel_main"></script></head>

  <body id="generic" class="" style="position: relative; min-height: 100%; top: 0px;">

    <div role="banner" id="top">
  <div class="container">
    <a class="logo" href="https://www.djangoproject.com/">Django</a>
    <p class="meta">The web framework for perfectionists with deadlines.</p>
    <div class="menu-button"><i class="icon icon-reorder"></i><span>Menu</span></div><div role="navigation" class="nav-menu-on">
      <ul>
        <li>
          <a href="https://www.djangoproject.com/start/overview/">Overview</a>
        </li>
        <li>
          <a href="https://www.djangoproject.com/download/">Download</a>
        </li>
        <li class="active">
          <a href="https://docs.djangoproject.com/">Documentation</a>
        </li>
        <li>
          <a href="https://www.djangoproject.com/weblog/">News</a>
        </li>
        <li>
          <a href="https://www.djangoproject.com/community/">Community</a>
        </li>
        <li>
          <a href="https://github.com/django/django" target="_blank" rel="noopener">Code</a>
        </li>
        <li>
          <a href="https://code.djangoproject.com/">Issues</a>
        </li>
        <li>
          <a href="https://www.djangoproject.com/foundation/">About</a>
        </li>
        <li>
          <a href="https://www.djangoproject.com/fundraising/">♥ Donate</a>
        </li>
      </ul>
    </div>
  </div>
</div>


    <div class="copy-banner">
      <div class="container 
container--flex container--flex--wrap--mobile
">
        
<h1><a href="https://docs.djangoproject.com/zh-hans/3.2/">文档</a></h1>


<form action="https://docs.djangoproject.com/zh-hans/3.2/search/" class="search form-input" role="search">
  <label class="visuallyhidden" for="id_q">Search:</label>
    <input type="search" name="q" placeholder="搜索 3.2 文档" id="id_q">

    <button type="submit">
      <i class="icon icon-search"></i>
      <span class="visuallyhidden">搜索</span>
    </button>
</form>


      </div>
    </div>
    <div id="billboard"></div>

    <div class="container sidebar-right">
      <div role="main">

        
          
        

        
<div id="version-switcher">
  <ul id="faq-link">
    <li class="current-link">
      <a href="https://docs.djangoproject.com/zh-hans/3.2/faq/help/">
        <span>Getting Help</span>
      </a>
    </li>
  </ul>
  <ul id="doc-languages" class="language-switcher">
  
  
  <li class="other">
    
      
    
    <a href="https://docs.djangoproject.com/el/3.2/ref/templates/builtins/">el</a>
  </li>
  
  
  
  <li class="other">
    
      
    
    <a href="https://docs.djangoproject.com/en/3.2/ref/templates/builtins/">en</a>
  </li>
  
  
  
  <li class="other">
    
      
    
    <a href="https://docs.djangoproject.com/es/3.2/ref/templates/builtins/">es</a>
  </li>
  
  
  
  <li class="other">
    
      
    
    <a href="https://docs.djangoproject.com/fr/3.2/ref/templates/builtins/">fr</a>
  </li>
  
  
  
  <li class="other">
    
      
    
    <a href="https://docs.djangoproject.com/id/3.2/ref/templates/builtins/">id</a>
  </li>
  
  
  
  <li class="other">
    
      
    
    <a href="https://docs.djangoproject.com/it/3.2/ref/templates/builtins/">it</a>
  </li>
  
  
  
  <li class="other">
    
      
    
    <a href="https://docs.djangoproject.com/ja/3.2/ref/templates/builtins/">ja</a>
  </li>
  
  
  
  <li class="other">
    
      
    
    <a href="https://docs.djangoproject.com/ko/3.2/ref/templates/builtins/">ko</a>
  </li>
  
  
  
  <li class="other">
    
      
    
    <a href="https://docs.djangoproject.com/pl/3.2/ref/templates/builtins/">pl</a>
  </li>
  
  
  
  <li class="other">
    
      
    
    <a href="https://docs.djangoproject.com/pt-br/3.2/ref/templates/builtins/">pt-br</a>
  </li>
  
  
  
  
    <li class="current" title="点击左侧的链接以切换到另一种语言。">
      <span>语言： <strong>zh-hans</strong></span>
    </li>
  </ul>

  
  <ul id="doc-versions" class="version-switcher">
    
    
    <li class="other">
      
        
      
      <a href="https://docs.djangoproject.com/zh-hans/2.0/ref/templates/builtins/">2.0</a>
    </li>
    
    
    
    <li class="other">
      
        
      
      <a href="https://docs.djangoproject.com/zh-hans/2.1/ref/templates/builtins/">2.1</a>
    </li>
    
    
    
    <li class="other">
      
        
      
      <a href="https://docs.djangoproject.com/zh-hans/2.2/ref/templates/builtins/">2.2</a>
    </li>
    
    
    
    <li class="other">
      
        
      
      <a href="https://docs.djangoproject.com/zh-hans/3.0/ref/templates/builtins/">3.0</a>
    </li>
    
    
    
    <li class="other">
      
        
      
      <a href="https://docs.djangoproject.com/zh-hans/3.1/ref/templates/builtins/">3.1</a>
    </li>
    
    
    
    
    
    <li class="other">
      
        
      
      <a href="https://docs.djangoproject.com/zh-hans/4.0/ref/templates/builtins/">4.0</a>
    </li>
    
    
    
    <li class="other">
      
        
      
      <a href="https://docs.djangoproject.com/zh-hans/dev/ref/templates/builtins/">dev</a>
    </li>
    
    
    <li class="current" title="此文档是为 Django 3.2 编写的。 点击左侧的链接查看其它版本">
       <span>文档版本：
         <strong>3.2</strong>
       </span>
    </li>
  </ul>
</div>


<div id="docs-content">
<div class="section" id="s-built-in-template-tags-and-filters">
<span id="built-in-template-tags-and-filters"></span><h1>内置模板标签和过滤器<a class="headerlink" href="#built-in-template-tags-and-filters" title="永久链接至标题">¶</a></h1>
<p>本文档描述了 Django 内置的模板标签和过滤器。如果有的话，建议你使用 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/contrib/admin/admindocs/"><span class="doc">自动化文档</span></a>，因为它也会包含安装的任何自定义标签或过滤器的文档。</p>
<div class="section" id="s-built-in-tag-reference">
<span id="s-ref-templates-builtins-tags"></span><span id="built-in-tag-reference"></span><span id="ref-templates-builtins-tags"></span><h2>内置标签参考<a class="headerlink" href="#built-in-tag-reference" title="永久链接至标题">¶</a></h2>
<div class="section" id="s-autoescape">
<span id="s-std:templatetag-autoescape"></span><span id="autoescape"></span><span id="std:templatetag-autoescape"></span><h3><code class="docutils literal notranslate"><span class="pre">autoescape</span></code><a class="headerlink" href="#autoescape" title="永久链接至标题">¶</a></h3>
<p>控制当前的自动转义行为。该标签以 <code class="docutils literal notranslate"><span class="pre">on</span></code> 或 <code class="docutils literal notranslate"><span class="pre">off</span></code> 作为参数，决定块内是否有自动转义行为。此区块以 <code class="docutils literal notranslate"><span class="pre">endautoescape</span></code> 结束标签关闭。</p>
<p>当自动转义生效时，所有的变量内容在将结果放入输出之前（但在任何过滤器被应用之后）都会被应用 HTML 转义。这相当于对每个变量手动应用 <a class="reference internal" href="#std:templatefilter-escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escape</span></code></a> 过滤器。</p>
<p>唯一的例外是那些已经被标记为 “安全” 的变量，这些变量可以是被填充变量的代码标记的，也可以是被应用了 <a class="reference internal" href="#std:templatefilter-safe"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">safe</span></code></a> 或 <a class="reference internal" href="#std:templatefilter-escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escape</span></code></a> 过滤器的。</p>
<p>示例用法：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">autoescape</span> <span class="nv">on</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">body</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-block">
<span id="s-std:templatetag-block"></span><span id="block"></span><span id="std:templatetag-block"></span><h3><code class="docutils literal notranslate"><span class="pre">block</span></code><a class="headerlink" href="#block" title="永久链接至标题">¶</a></h3>
<p>定义一个可以被子模板覆盖的块。更多信息请参见 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/templates/language/#template-inheritance"><span class="std std-ref">模板继承</span></a>。</p>
</div>
<div class="section" id="s-comment">
<span id="s-std:templatetag-comment"></span><span id="comment"></span><span id="std:templatetag-comment"></span><h3><code class="docutils literal notranslate"><span class="pre">comment</span></code><a class="headerlink" href="#comment" title="永久链接至标题">¶</a></h3>
<p>忽略 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">comment</span> <span class="pre">%}</span></code> 和 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">endcomment</span> <span class="pre">%}</span></code> 之间的所有内容。可以在第一个标签中插入一个可选的注释。例如，这在注释代码时很有用，可以记录代码被禁用的原因。</p>
<p>示例用法：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>Rendered text with <span class="cp">{{</span> <span class="nv">pub_date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"c"</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">comment</span> <span class="s2">"Optional note"</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>Commented out text with <span class="cp">{{</span> <span class="nv">create_date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"c"</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endcomment</span> <span class="cp">%}</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">comment</span></code> 标签不能嵌套。</p>
</div>
<div class="section" id="s-csrf-token">
<span id="s-std:templatetag-csrf_token"></span><span id="csrf-token"></span><span id="std:templatetag-csrf_token"></span><h3><code class="docutils literal notranslate"><span class="pre">csrf_token</span></code><a class="headerlink" href="#csrf-token" title="永久链接至标题">¶</a></h3>
<p>这个标签用于 CSRF 保护，如 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/csrf/"><span class="doc">跨站点请求伪造</span></a> 的文档中所述。</p>
</div>
<div class="section" id="s-cycle">
<span id="s-std:templatetag-cycle"></span><span id="cycle"></span><span id="std:templatetag-cycle"></span><h3><code class="docutils literal notranslate"><span class="pre">cycle</span></code><a class="headerlink" href="#cycle" title="永久链接至标题">¶</a></h3>
<p>每次遇到这个标记时，都会产生一个参数。第一次遇到时产生第一个参数，第二次遇到时产生第二个参数，以此类推。一旦用尽所有参数，标签就会循环到第一个参数，并再次生成它。</p>
<p>这个标签在循环中特别有用：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">o</span> <span class="k">in</span> <span class="nv">some_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">tr</span> <span class="na">class</span><span class="o">=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'row1'</span> <span class="s1">'row2'</span> <span class="cp">%}</span><span class="s">"</span><span class="p">&gt;</span>
        ...
    <span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>第一次迭代产生的 HTML 引用类 <code class="docutils literal notranslate"><span class="pre">row1</span></code>，第二次引用 <code class="docutils literal notranslate"><span class="pre">row2</span></code>，第三次再引用 <code class="docutils literal notranslate"><span class="pre">row1</span></code>，以此类推，每次循环迭代。</p>
<p>你也可以使用变量。例如，如果你有两个模板变量，<code class="docutils literal notranslate"><span class="pre">rowvalue1</span></code> 和 <code class="docutils literal notranslate"><span class="pre">rowvalue2</span></code>，你可以像这样交替使用它们的值：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">o</span> <span class="k">in</span> <span class="nv">some_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">tr</span> <span class="na">class</span><span class="o">=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">rowvalue1</span> <span class="nv">rowvalue2</span> <span class="cp">%}</span><span class="s">"</span><span class="p">&gt;</span>
        ...
    <span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>包含在循环中的变量将被转义。 你可以通过以下方式禁用自动转义：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">o</span> <span class="k">in</span> <span class="nv">some_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">tr</span> <span class="na">class</span><span class="o">=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">autoescape</span> <span class="nv">off</span> <span class="cp">%}{%</span> <span class="k">cycle</span> <span class="nv">rowvalue1</span> <span class="nv">rowvalue2</span> <span class="cp">%}{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span><span class="s">"</span><span class="p">&gt;</span>
        ...
    <span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>你可以混合变量和字符串：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">o</span> <span class="k">in</span> <span class="nv">some_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">tr</span> <span class="na">class</span><span class="o">=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'row1'</span> <span class="nv">rowvalue2</span> <span class="s1">'row3'</span> <span class="cp">%}</span><span class="s">"</span><span class="p">&gt;</span>
        ...
    <span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>在某些情况下，你可能想引用一个周期的当前值，而不是前进到下一个值。要做到这一点，请使用 “as” 给 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></code> 标签命名，比如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'row1'</span> <span class="s1">'row2'</span> <span class="k">as</span> <span class="nv">rowcolors</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>从那时起，你可以通过引用循环名称作为上下文变量，在模板中任何地方插入循环的当前值。如果你想把循环移动到下一个值，独立于原来的 <code class="docutils literal notranslate"><span class="pre">cycle</span></code> 标签，你可以使用另一个 <code class="docutils literal notranslate"><span class="pre">cycle</span></code> 标签，并指定变量的名称。所以，下面的模板：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">&lt;</span><span class="nt">tr</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'row1'</span> <span class="s1">'row2'</span> <span class="k">as</span> <span class="nv">rowcolors</span> <span class="cp">%}</span><span class="s">"</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">rowcolors</span> <span class="cp">}}</span><span class="s">"</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">tr</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">rowcolors</span> <span class="cp">%}</span><span class="s">"</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">rowcolors</span> <span class="cp">}}</span><span class="s">"</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>将会输出：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">&lt;</span><span class="nt">tr</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">"row1"</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">"row1"</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">tr</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">"row2"</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
    <span class="p">&lt;</span><span class="nt">td</span> <span class="na">class</span><span class="o">=</span><span class="s">"row2"</span><span class="p">&gt;</span>...<span class="p">&lt;/</span><span class="nt">td</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>你可以在一个 <code class="docutils literal notranslate"><span class="pre">cycle</span></code> 标签中使用任意数量的值，用空格隔开。用单引号（<code class="docutils literal notranslate"><span class="pre">'</span></code>）或双引号（<code class="docutils literal notranslate"><span class="pre">"</span></code>）括起来的值被视为字符串字面，而没有引号的值被视为模板变量。</p>
<p>默认情况下，当你在循环标签中使用 <code class="docutils literal notranslate"><span class="pre">as</span></code> 关键字时，启动循环的 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></code> 的用法将自己产生循环中的第一个值。如果你想在嵌套循环或包含的模板中使用该值，这可能是一个问题。如果你只想声明循环，但不产生第一个值，您可以在标签中添加一个 <code class="docutils literal notranslate"><span class="pre">silent</span></code> 关键字作为最后一个关键字。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">obj</span> <span class="k">in</span> <span class="nv">some_list</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'row1'</span> <span class="s1">'row2'</span> <span class="k">as</span> <span class="nv">rowcolors</span> <span class="nv">silent</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">tr</span> <span class="na">class</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">rowcolors</span> <span class="cp">}}</span><span class="s">"</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">include</span> <span class="s2">"subtemplate.html"</span> <span class="cp">%}</span><span class="p">&lt;/</span><span class="nt">tr</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>这将输出一个 <code class="docutils literal notranslate"><span class="pre">&lt;tr&gt;</span></code> 元素的列表，其 <code class="docutils literal notranslate"><span class="pre">class</span></code> 在 <code class="docutils literal notranslate"><span class="pre">row1</span></code> 和 <code class="docutils literal notranslate"><span class="pre">row2</span></code> 之间交替出现。子模板在其上下文中可以访问 <code class="docutils literal notranslate"><span class="pre">rowcolors</span></code>，其值将与包围它的 <code class="docutils literal notranslate"><span class="pre">&lt;tr&gt;</span></code> 的类相匹配。如果省略 <code class="docutils literal notranslate"><span class="pre">silent</span></code> 关键字，<code class="docutils literal notranslate"><span class="pre">row1</span></code> 和 <code class="docutils literal notranslate"><span class="pre">row2</span></code> 将作为普通文本在 <code class="docutils literal notranslate"><span class="pre">&lt;tr&gt;</span></code> 元素之外发出。</p>
<p>当在循环定义上使用 <code class="docutils literal notranslate"><span class="pre">silent</span></code> 关键字时，<code class="docutils literal notranslate"><span class="pre">silent</span></code> 自动适用于该特定循环标记的所有后续使用。以下模板将 <em>不</em> 输出任何东西，即使第二次调用 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></code> 并没有指定 <code class="docutils literal notranslate"><span class="pre">silent</span></code> ：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'row1'</span> <span class="s1">'row2'</span> <span class="k">as</span> <span class="nv">rowcolors</span> <span class="nv">silent</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">cycle</span> <span class="nv">rowcolors</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>你可以使用 <a class="reference internal" href="#std:templatetag-resetcycle"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">resetcycle</span></code></a> 标签使 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></code> 标签在下次遇到时从第一个值重新开始。</p>
</div>
<div class="section" id="s-debug">
<span id="s-std:templatetag-debug"></span><span id="debug"></span><span id="std:templatetag-debug"></span><h3><code class="docutils literal notranslate"><span class="pre">debug</span></code><a class="headerlink" href="#debug" title="永久链接至标题">¶</a></h3>
<p>输出整体的调试信息，包括当前上下文和导入的模块。</p>
</div>
<div class="section" id="s-extends">
<span id="s-std:templatetag-extends"></span><span id="extends"></span><span id="std:templatetag-extends"></span><h3><code class="docutils literal notranslate"><span class="pre">extends</span></code><a class="headerlink" href="#extends" title="永久链接至标题">¶</a></h3>
<p>表示该模板扩展了一个父模板。</p>
<p>这个标签有两种使用方式：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">"base.html"</span> <span class="pre">%}</span></code> （带引号）使用字面值 <code class="docutils literal notranslate"><span class="pre">"base.html"</span></code> 作为要扩展的父模板的名称。</li>
<li><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">variable</span> <span class="pre">%}</span></code> 使用 <code class="docutils literal notranslate"><span class="pre">variable</span></code> 的值。如果变量的值是一个字符串，Django 将使用这个字符串作为父模板的名称。如果变量的值是一个 <code class="docutils literal notranslate"><span class="pre">Template</span></code> 对象，Django 将使用该对象作为父模板。</li>
</ul>
<p>更多内容请参见 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/templates/language/#template-inheritance"><span class="std std-ref">模板继承</span></a> 文档.</p>
<p>通常模板名称是相对于模板加载器的根目录而言的。字符串参数也可以是以 <code class="docutils literal notranslate"><span class="pre">./</span></code> 或 <code class="docutils literal notranslate"><span class="pre">../</span></code> 开头的相对路径。例如，假设以下目录结构：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span>dir1/
    template.html
    base2.html
    my/
        base3.html
base1.html
</pre></div>
</div>
<p>在 <code class="docutils literal notranslate"><span class="pre">template.html</span></code> 中，以下路径将是有效的：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">"./base2.html"</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">extends</span> <span class="s2">"../base1.html"</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">extends</span> <span class="s2">"./my/base3.html"</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-filter">
<span id="s-std:templatetag-filter"></span><span id="filter"></span><span id="std:templatetag-filter"></span><h3><code class="docutils literal notranslate"><span class="pre">filter</span></code><a class="headerlink" href="#filter" title="永久链接至标题">¶</a></h3>
<p>通过一个或多个过滤器过滤块的内容。可以用管道指定多个过滤器，过滤器可以有参数，就像变量语法一样。</p>
<p>请注意，该块包括 <code class="docutils literal notranslate"><span class="pre">filter</span></code> 和 <code class="docutils literal notranslate"><span class="pre">endfilter</span></code> 标签之间的 <em>所有</em> 文本。</p>
<p>示例用法：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">filter</span> <span class="nf">force_escape</span><span class="o">|</span><span class="nf">lower</span> <span class="cp">%}</span>
    This text will be HTML-escaped, and will appear in all lowercase.
<span class="cp">{%</span> <span class="k">endfilter</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last"><a class="reference internal" href="#std:templatefilter-escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escape</span></code></a> 和 <a class="reference internal" href="#std:templatefilter-safe"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">safe</span></code></a> 过滤器是不可接受的参数。取而代之的是，使用 <a class="reference internal" href="#std:templatetag-autoescape"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">autoescape</span></code></a> 标签来管理模板代码块的自动转义。</p>
</div>
</div>
<div class="section" id="s-firstof">
<span id="s-std:templatetag-firstof"></span><span id="firstof"></span><span id="std:templatetag-firstof"></span><h3><code class="docutils literal notranslate"><span class="pre">firstof</span></code><a class="headerlink" href="#firstof" title="永久链接至标题">¶</a></h3>
<p>输出第一个不是 <code class="docutils literal notranslate"><span class="pre">false</span></code> 的参数变量（即存在、不为空、不是一个错误的布尔值，也不是一个零的数值）。如果所有传递的变量都是 <code class="docutils literal notranslate"><span class="pre">false</span></code>，则不输出任何内容。</p>
<p>示例用法：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">firstof</span> <span class="nv">var1</span> <span class="nv">var2</span> <span class="nv">var3</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>这相当于：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">var1</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">var1</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">elif</span> <span class="nv">var2</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">var2</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">elif</span> <span class="nv">var3</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">var3</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>你也可以在所有传递的变量都是 False 的情况下使用一个字符串作为后备值：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">firstof</span> <span class="nv">var1</span> <span class="nv">var2</span> <span class="nv">var3</span> <span class="s2">"fallback value"</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>这个标签会自动转义变量值。你可以通过以下方式禁用自动转义：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">autoescape</span> <span class="nv">off</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">firstof</span> <span class="nv">var1</span> <span class="nv">var2</span> <span class="nv">var3</span> <span class="s2">"&lt;strong&gt;fallback value&lt;/strong&gt;"</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>或者如果只对一些变量进行转义，可以使用：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">firstof</span> <span class="nv">var1</span> <span class="nv">var2</span><span class="o">|</span><span class="nf">safe</span> <span class="nv">var3</span> <span class="s2">"&lt;strong&gt;fallback value&lt;/strong&gt;"</span><span class="o">|</span><span class="nf">safe</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>你可以使用 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">firstof</span> <span class="pre">var1</span> <span class="pre">var2</span> <span class="pre">var3</span> <span class="pre">as</span> <span class="pre">value</span> <span class="pre">%}</span></code> 的语法将输出结果存储在一个变量中。</p>
</div>
<div class="section" id="s-for">
<span id="s-std:templatetag-for"></span><span id="for"></span><span id="std:templatetag-for"></span><h3><code class="docutils literal notranslate"><span class="pre">for</span></code><a class="headerlink" href="#for" title="永久链接至标题">¶</a></h3>
<p>循环浏览数组中的每个项目，使该项目在上下文变量中可用。例如，要显示 <code class="docutils literal notranslate"><span class="pre">athlete_list</span></code> 中提供的运动员列表：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">athlete</span> <span class="k">in</span> <span class="nv">athlete_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">athlete.name</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>你可以通过使用 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">for</span> <span class="pre">obj</span> <span class="pre">in</span> <span class="pre">list</span> <span class="pre">reversed</span> <span class="pre">%}</span></code> 来反向循环一个列表。</p>
<p>如果你需要对一个列表进行循环，你可以将每个子列表中的值解包为单个变量。例如，如果你的上下文中包含一个名为 <code class="docutils literal notranslate"><span class="pre">points</span></code> 的 (x,y) 坐标列表，你可以使用下面的方法来输出点的列表：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">x</span><span class="o">,</span> <span class="nv">y</span> <span class="k">in</span> <span class="nv">points</span> <span class="cp">%}</span>
    There is a point at <span class="cp">{{</span> <span class="nv">x</span> <span class="cp">}}</span>,<span class="cp">{{</span> <span class="nv">y</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>如果你需要访问字典中的项目，这也很有用。例如，如果你的上下文中包含一个字典 <code class="docutils literal notranslate"><span class="pre">data</span></code>，下面将显示该字典的键和值：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">key</span><span class="o">,</span> <span class="nv">value</span> <span class="k">in</span> <span class="nv">data.items</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">key</span> <span class="cp">}}</span>: <span class="cp">{{</span> <span class="nv">value</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>请记住，对于点运算符来说，字典键查询优先于方法查询。因此，如果 <code class="docutils literal notranslate"><span class="pre">data</span></code> 字典中包含一个名为 <code class="docutils literal notranslate"><span class="pre">'items'</span></code> 的键，<code class="docutils literal notranslate"><span class="pre">data.items</span></code> 将返回 <code class="docutils literal notranslate"><span class="pre">data['items']`</span></code> 而不是 <code class="docutils literal notranslate"><span class="pre">data.items()</span></code>。如果你想在模板中使用这些方法，避免添加像字典方法一样命名的键（<code class="docutils literal notranslate"><span class="pre">items</span></code>、<code class="docutils literal notranslate"><span class="pre">values</span></code>、<code class="docutils literal notranslate"><span class="pre">keys</span></code> 等）。在 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/templates/language/#template-variables"><span class="std std-ref">模板变量的文档</span></a> 中阅读更多关于点运算符的查找顺序。</p>
<p>for 循环设置了一组可以在循环体内直接使用的变量：</p>
<table class="docutils">
<colgroup>
<col width="36%">
<col width="64%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">变量名</th>
<th class="head">描述</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">forloop.counter</span></code></td>
<td>循环计数器，表示当前循环的索引（从 <code class="docutils literal notranslate"><span class="pre">1</span></code> 开始）。</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">forloop.counter0</span></code></td>
<td>循环计数器，表示当前循环的索引（从 <code class="docutils literal notranslate"><span class="pre">0</span></code> 开始）。</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">forloop.revcounter</span></code></td>
<td>反向循环计数器（以最后一次循环为 <code class="docutils literal notranslate"><span class="pre">1</span></code>，反向计数）。</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">forloop.revcounter0</span></code></td>
<td>反向循环计数器（以最后一次循环为 <code class="docutils literal notranslate"><span class="pre">0</span></code>，反向计数）。</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">forloop.first</span></code></td>
<td>当前循环为首个循环时，该变量为 True</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">forloop.last</span></code></td>
<td>当前循环为最后一个循环时，该变量为 True</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">forloop.parentloop</span></code></td>
<td>在嵌套循环中，指向当前循环的上级循环</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="s-for-empty">
<span id="for-empty"></span><h3><code class="docutils literal notranslate"><span class="pre">for</span></code> ... <code class="docutils literal notranslate"><span class="pre">empty</span></code><a class="headerlink" href="#for-empty" title="永久链接至标题">¶</a></h3>
<p>当传递到 <code class="docutils literal notranslate"><span class="pre">for</span></code> 标签中的数组不存在或为空时，可以使用 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">empty</span> <span class="pre">%}</span></code> 标签来指定输出的内容:</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">athlete</span> <span class="k">in</span> <span class="nv">athlete_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">athlete.name</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">empty</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>Sorry, no athletes in this list.<span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>上面的代码与下面的代码是等同的，但上面的代码更简短，更清晰，也可能更快。</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
  <span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">for</span> <span class="nv">athlete</span> <span class="k">in</span> <span class="nv">athlete_list</span> <span class="cp">%}</span>
      <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">athlete.name</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
  <span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>Sorry, no athletes in this list.<span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
  <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-if">
<span id="s-std:templatetag-if"></span><span id="if"></span><span id="std:templatetag-if"></span><h3><code class="docutils literal notranslate"><span class="pre">if</span></code><a class="headerlink" href="#if" title="永久链接至标题">¶</a></h3>
<p><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">if</span> <span class="pre">%}</span></code> 标签会判断给定的变量，当变量为 True 时（比如存在、非空、非布尔值 False），就会输出块内的内容：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="cp">%}</span>
    Number of athletes: <span class="cp">{{</span> <span class="nv">athlete_list</span><span class="o">|</span><span class="nf">length</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">elif</span> <span class="nv">athlete_in_locker_room_list</span> <span class="cp">%}</span>
    Athletes should be out of the locker room soon!
<span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span>
    No athletes.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>在上面的例子中, 如果 <code class="docutils literal notranslate"><span class="pre">athlete_list</span></code> 不是空的, 那么变量 <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">athlete_list|length</span> <span class="pre">}}</span></code> 就会被显示出来.</p>
<p>正如你所看到的，<code class="docutils literal notranslate"><span class="pre">if</span></code> 标签可能带有一个或多个 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">elif</span> <span class="pre">%}</span></code> 分支，以及一个 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">else</span> <span class="pre">%}</span></code> 分支。当 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">else</span> <span class="pre">%}</span></code> 之前的所有分支条件都不满足时，<code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">else</span> <span class="pre">%}</span></code> 分支的内容会被显示出来。所有的分支都是可选的。</p>
<div class="section" id="s-boolean-operators">
<span id="boolean-operators"></span><h4>布尔操作<a class="headerlink" href="#boolean-operators" title="永久链接至标题">¶</a></h4>
<p><a class="reference internal" href="#std:templatetag-if"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">if</span></code></a> 标签可以使用 <code class="docutils literal notranslate"><span class="pre">and</span></code>、<code class="docutils literal notranslate"><span class="pre">or</span></code> 或 <code class="docutils literal notranslate"><span class="pre">not</span></code> 来测试一些变量或取反某个变量：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="k">and</span> <span class="nv">coach_list</span> <span class="cp">%}</span>
    Both athletes and coaches are available.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="k">not</span> <span class="nv">athlete_list</span> <span class="cp">%}</span>
    There are no athletes.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="k">or</span> <span class="nv">coach_list</span> <span class="cp">%}</span>
    There are some athletes or some coaches.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="k">not</span> <span class="nv">athlete_list</span> <span class="k">or</span> <span class="nv">coach_list</span> <span class="cp">%}</span>
    There are no athletes or there are some coaches.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="k">and</span> <span class="k">not</span> <span class="nv">coach_list</span> <span class="cp">%}</span>
    There are some athletes and absolutely no coaches.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>允许在同一标签中同时使用 <code class="docutils literal notranslate"><span class="pre">and</span></code> 和 <code class="docutils literal notranslate"><span class="pre">or</span></code> 子句，<code class="docutils literal notranslate"><span class="pre">and</span></code> 比 <code class="docutils literal notranslate"><span class="pre">or</span></code> 优先级高，例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">athlete_list</span> <span class="k">and</span> <span class="nv">coach_list</span> <span class="k">or</span> <span class="nv">cheerleader_list</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>将被解释为：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="k">if</span> <span class="p">(</span><span class="n">athlete_list</span> <span class="ow">and</span> <span class="n">coach_list</span><span class="p">)</span> <span class="ow">or</span> <span class="n">cheerleader_list</span>
</pre></div>
</div>
<p>在 <a class="reference internal" href="#std:templatetag-if"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">if</span></code></a> 标签中使用实际的括号是无效的语法。如果需要小括号来表示优先级，应使用嵌套的 <a class="reference internal" href="#std:templatetag-if"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">if</span></code></a> 标签。</p>
<p><a class="reference internal" href="#std:templatetag-if"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">if</span></code></a> 标签也可以使用运算符 <code class="docutils literal notranslate"><span class="pre">==</span></code>、<code class="docutils literal notranslate"><span class="pre">!=</span></code>、<code class="docutils literal notranslate"><span class="pre">&lt;</span></code>、<code class="docutils literal notranslate"><span class="pre">&gt;</span></code>、<code class="docutils literal notranslate"><span class="pre">&lt;=</span></code>、<code class="docutils literal notranslate"><span class="pre">&gt;=</span></code>、<code class="docutils literal notranslate"><span class="pre">in</span></code>、<code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">in</span></code>、<code class="docutils literal notranslate"><span class="pre">is</span></code> 和 <code class="docutils literal notranslate"><span class="pre">`is</span> <span class="pre">not</span></code>，其作用如下：</p>
<div class="section" id="s-operator">
<span id="operator"></span><h5><code class="docutils literal notranslate"><span class="pre">==</span></code> 运算符<a class="headerlink" href="#operator" title="永久链接至标题">¶</a></h5>
<p>相等。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">==</span> <span class="s2">"x"</span> <span class="cp">%}</span>
  This appears if variable somevar equals the string "x"
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-id1">
<span id="id1"></span><h5><code class="docutils literal notranslate"><span class="pre">!=</span></code> 运算符<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h5>
<p>不相等。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">!=</span> <span class="s2">"x"</span> <span class="cp">%}</span>
  This appears if variable somevar does not equal the string "x",
  or if somevar is not found in the context
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-id2">
<span id="id2"></span><h5><code class="docutils literal notranslate"><span class="pre">&lt;</span></code> 运算符<a class="headerlink" href="#id2" title="永久链接至标题">¶</a></h5>
<p>小于。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">&lt;</span> <span class="m">100</span> <span class="cp">%}</span>
  This appears if variable somevar is less than 100.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-id3">
<span id="id3"></span><h5><code class="docutils literal notranslate"><span class="pre">&gt;</span></code> 运算符<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h5>
<p>大于。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">&gt;</span> <span class="m">0</span> <span class="cp">%}</span>
  This appears if variable somevar is greater than 0.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-id4">
<span id="id4"></span><h5><code class="docutils literal notranslate"><span class="pre">&lt;=</span></code> 运算符<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h5>
<p>小于等于。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">&lt;=</span> <span class="m">100</span> <span class="cp">%}</span>
  This appears if variable somevar is less than 100 or equal to 100.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-id5">
<span id="id5"></span><h5><code class="docutils literal notranslate"><span class="pre">&gt;=</span></code> 运算符<a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h5>
<p>大于等于。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="o">&gt;=</span> <span class="m">1</span> <span class="cp">%}</span>
  This appears if variable somevar is greater than 1 or equal to 1.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-in-operator">
<span id="in-operator"></span><h5><code class="docutils literal notranslate"><span class="pre">in</span></code> 运算符<a class="headerlink" href="#in-operator" title="永久链接至标题">¶</a></h5>
<p>包含其中。许多 Python 容器都支持这个运算符来测试给定值是否在容器中。下面是一些如何解释 <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></code> 的例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="s2">"bc"</span> <span class="k">in</span> <span class="s2">"abcdef"</span> <span class="cp">%}</span>
  This appears since "bc" is a substring of "abcdef"
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="s2">"hello"</span> <span class="k">in</span> <span class="nv">greetings</span> <span class="cp">%}</span>
  If greetings is a list or set, one element of which is the string
  "hello", this will appear.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="nv">user</span> <span class="k">in</span> <span class="nv">users</span> <span class="cp">%}</span>
  If users is a QuerySet, this will appear if user is an
  instance that belongs to the QuerySet.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-not-in-operator">
<span id="not-in-operator"></span><h5><code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">in</span></code> 操作符<a class="headerlink" href="#not-in-operator" title="永久链接至标题">¶</a></h5>
<p>不包含在其中。这是 <code class="docutils literal notranslate"><span class="pre">in</span></code> 运算符的取反。</p>
</div>
<div class="section" id="s-is-operator">
<span id="is-operator"></span><h5><code class="docutils literal notranslate"><span class="pre">is</span></code> 运算符<a class="headerlink" href="#is-operator" title="永久链接至标题">¶</a></h5>
<p>对象检测。测试两个变量是否为同一个对象。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="k">is</span> <span class="nf">True</span> <span class="cp">%}</span>
  This appears if and only if somevar is True.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="k">is</span> <span class="nf">None</span> <span class="cp">%}</span>
  This appears if somevar is None, or if somevar is not found in the context.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-is-not-operator">
<span id="is-not-operator"></span><h5><code class="docutils literal notranslate"><span class="pre">is</span> <span class="pre">not</span></code> 运算符<a class="headerlink" href="#is-not-operator" title="永久链接至标题">¶</a></h5>
<p>对象检测后取反。测试两个变量是否不是同一个对象。这是对 <code class="docutils literal notranslate"><span class="pre">is</span></code> 运算符的取反。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="k">is</span> <span class="k">not</span> <span class="nf">True</span> <span class="cp">%}</span>
  This appears if somevar is not True, or if somevar is not found in the
  context.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>

<span class="cp">{%</span> <span class="k">if</span> <span class="nv">somevar</span> <span class="k">is</span> <span class="k">not</span> <span class="nf">None</span> <span class="cp">%}</span>
  This appears if and only if somevar is not None.
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-filters">
<span id="filters"></span><h4>过滤器<a class="headerlink" href="#filters" title="永久链接至标题">¶</a></h4>
<p>你也可以在 <a class="reference internal" href="#std:templatetag-if"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">if</span></code></a> 表达式中使用过滤器。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">messages</span><span class="o">|</span><span class="nf">length</span> <span class="o">&gt;=</span> <span class="m">100</span> <span class="cp">%}</span>
   You have lots of messages today!
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-complex-expressions">
<span id="complex-expressions"></span><h4>复合表达式<a class="headerlink" href="#complex-expressions" title="永久链接至标题">¶</a></h4>
<p>以上所有内容都可以组合成复合表达式。对于这样的表达式，了解表达式计算时运算符的分组方式——也就是优先规则，可能很重要。运算符的优先级，从低到高，如下所示：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">or</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">and</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">not</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">in</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">==</span></code>、<code class="docutils literal notranslate"><span class="pre">!=</span></code>、<code class="docutils literal notranslate"><span class="pre">&lt;</span></code>、<code class="docutils literal notranslate"><span class="pre">&gt;</span></code>、<code class="docutils literal notranslate"><span class="pre">&lt;=</span></code>、<code class="docutils literal notranslate"><span class="pre">&gt;=</span></code></li>
</ul>
<p>（这完全是按照 Python 来的）。所以，例如，以下复杂的 <a class="reference internal" href="#std:templatetag-if"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">if</span></code></a> 标签：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">a</span> <span class="o">==</span> <span class="nv">b</span> <span class="k">or</span> <span class="nv">c</span> <span class="o">==</span> <span class="nv">d</span> <span class="k">and</span> <span class="nv">e</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>...将被解释为：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="p">)</span> <span class="ow">or</span> <span class="p">((</span><span class="n">c</span> <span class="o">==</span> <span class="n">d</span><span class="p">)</span> <span class="ow">and</span> <span class="n">e</span><span class="p">)</span>
</pre></div>
</div>
<p>如果你需要不同的优先级，你需要使用嵌套的 <a class="reference internal" href="#std:templatetag-if"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">if</span></code></a> 标签。有时，为了不了解优先规则的人，这样做还是比较清楚的。</p>
<p>比较运算符不能像在 Python 或数学符号中那样 “连锁”。例如，不能使用：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">a</span> <span class="o">&gt;</span> <span class="nv">b</span> <span class="o">&gt;</span> <span class="nv">c</span> <span class="cp">%}</span>  (WRONG)
</pre></div>
</div>
<p>你应该使用：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">a</span> <span class="o">&gt;</span> <span class="nv">b</span> <span class="k">and</span> <span class="nv">b</span> <span class="o">&gt;</span> <span class="nv">c</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-ifequal-and-ifnotequal">
<span id="ifequal-and-ifnotequal"></span><h3><code class="docutils literal notranslate"><span class="pre">ifequal</span></code> 和 <code class="docutils literal notranslate"><span class="pre">ifnotequal</span></code><a class="headerlink" href="#ifequal-and-ifnotequal" title="永久链接至标题">¶</a></h3>
<div class="deprecated">
<p><span class="versionmodified">3.1 版后已移除.</span></p>
</div>
<p><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">ifequal</span> <span class="pre">a</span> <span class="pre">b</span> <span class="pre">%}</span> <span class="pre">...</span> <span class="pre">{%</span> <span class="pre">endifequal</span> <span class="pre">%}</span></code> 是写 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">if</span> <span class="pre">a</span> <span class="pre">==</span> <span class="pre">b</span> <span class="pre">%}</span> <span class="pre">...</span> <span class="pre">{%</span> <span class="pre">endif</span> <span class="pre">%}</span></code> 的过时方式。同样，<code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">ifnotequal</span> <span class="pre">a</span> <span class="pre">b</span> <span class="pre">%}</span> <span class="pre">...</span> <span class="pre">{%</span> <span class="pre">endifnotequal</span> <span class="pre">%}</span></code> 被 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">if</span> <span class="pre">a</span> <span class="pre">!=</span> <span class="pre">b</span> <span class="pre">%}</span> <span class="pre">...</span> <span class="pre">{%</span> <span class="pre">endif</span> <span class="pre">%}</span></code> 取代。</p>
</div>
<div class="section" id="s-ifchanged">
<span id="s-std:templatetag-ifchanged"></span><span id="ifchanged"></span><span id="std:templatetag-ifchanged"></span><h3><code class="docutils literal notranslate"><span class="pre">ifchanged</span></code><a class="headerlink" href="#ifchanged" title="永久链接至标题">¶</a></h3>
<p>检查一个值是否在循环的最后一次迭代中发生了变化。</p>
<p><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">ifchanged</span> <span class="pre">%}</span></code> 块标签用于循环中。它有两种可能的用法。</p>
<ol class="arabic">
<li><p class="first">根据之前的状态检查自己渲染的内容，只有在内容发生变化时才显示。例如，这将显示一个天数列表，如果它改变了，只显示月份：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>Archive for <span class="cp">{{</span> <span class="nv">year</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>

<span class="cp">{%</span> <span class="k">for</span> <span class="nv">date</span> <span class="k">in</span> <span class="nv">days</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">ifchanged</span> <span class="cp">%}</span><span class="p">&lt;</span><span class="nt">h3</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"F"</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h3</span><span class="p">&gt;</span><span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"M/d"</span><span class="o">|</span><span class="nf">lower</span> <span class="cp">}}</span><span class="s">/"</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">date</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"j"</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
</li>
<li><p class="first">如果给定一个或多个变量，检查是否有任何变量发生了变化。例如，下面的例子显示了日期的每次变化，而如果小时或日期发生了变化，则显示小时：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">date</span> <span class="k">in</span> <span class="nv">days</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">ifchanged</span> <span class="nv">date.date</span> <span class="cp">%}</span> <span class="cp">{{</span> <span class="nv">date.date</span> <span class="cp">}}</span> <span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">ifchanged</span> <span class="nv">date.hour</span> <span class="nv">date.date</span> <span class="cp">%}</span>
        <span class="cp">{{</span> <span class="nv">date.hour</span> <span class="cp">}}</span>
    <span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
</li>
</ol>
<p><code class="docutils literal notranslate"><span class="pre">ifchanged</span></code> 标签也可以使用一个可选的 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">else</span> <span class="pre">%}</span></code> 子句，如果值没有改变，就会显示：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">match</span> <span class="k">in</span> <span class="nv">matches</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">div</span> <span class="na">style</span><span class="o">=</span><span class="s">"background-color:</span>
<span class="s">        </span><span class="cp">{%</span> <span class="k">ifchanged</span> <span class="nv">match.ballot_id</span> <span class="cp">%}</span><span class="s"></span>
<span class="s">            </span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s2">"red"</span> <span class="s2">"blue"</span> <span class="cp">%}</span><span class="s"></span>
<span class="s">        </span><span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span><span class="s"></span>
<span class="s">            gray</span>
<span class="s">        </span><span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span><span class="s"></span>
<span class="s">    "</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">match</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-include">
<span id="s-std:templatetag-include"></span><span id="include"></span><span id="std:templatetag-include"></span><h3><code class="docutils literal notranslate"><span class="pre">include</span></code><a class="headerlink" href="#include" title="永久链接至标题">¶</a></h3>
<p>加载一个模板，并在当前上下文中进行渲染。这是一种在模板中 “包含” 其他模板的方式。</p>
<p>模板名称可以是一个变量，也可以是一个硬编码（引号）字符串，可以是单引号或双引号。</p>
<p>这个例子包含模板 <code class="docutils literal notranslate"><span class="pre">"foo/bar.html"</span></code> 的内容：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">include</span> <span class="s2">"foo/bar.html"</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>通常模板名称是相对于模板加载器的根目录而言的。字符串参数也可以是以 <code class="docutils literal notranslate"><span class="pre">./</span></code> 或 <code class="docutils literal notranslate"><span class="pre">../</span></code> 开头的相对路径，如 <a class="reference internal" href="#std:templatetag-extends"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">extends</span></code></a> 标签所述。</p>
<p>这个例子包含模板的内容，其名称包含在变量 <code class="docutils literal notranslate"><span class="pre">template_name</span></code> ：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">include</span> <span class="nv">template_name</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>变量也可以是任何有 <code class="docutils literal notranslate"><span class="pre">render()</span></code> 方法并接受上下文的对象。这允许你在你的上下文中引用一个编译的 <code class="docutils literal notranslate"><span class="pre">Template</span></code>。</p>
<p>此外，该变量可以是模板名称的迭代，在这种情况下，将使用第一个可以加载的模板，如 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/topics/templates/#django.template.loader.select_template" title="django.template.loader.select_template"><code class="xref py py-func docutils literal notranslate"><span class="pre">select_template()</span></code></a>。</p>
<p>一个被包含的模板会在包含它的模板的上下文中呈现。这个例子产生的输出是 <code class="docutils literal notranslate"><span class="pre">"Hello,</span> <span class="pre">John!"</span></code>。</p>
<ul>
<li><p class="first">上下文：变量 <code class="docutils literal notranslate"><span class="pre">person</span></code> 设置为 <code class="docutils literal notranslate"><span class="pre">"John"</span></code>，变量 <code class="docutils literal notranslate"><span class="pre">greeting</span></code> 设置为 <code class="docutils literal notranslate"><span class="pre">"Hello"</span></code>。</p>
</li>
<li><p class="first">模板：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">include</span> <span class="s2">"name_snippet.html"</span> <span class="cp">%}</span>
</pre></div>
</div>
</li>
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">name_snippet.html</span></code> 模板：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">greeting</span> <span class="cp">}}</span>, <span class="cp">{{</span> <span class="nv">person</span><span class="o">|</span><span class="nf">default</span><span class="s2">:"friend"</span> <span class="cp">}}</span>!
</pre></div>
</div>
</li>
</ul>
<p>你可以使用关键字参数向模板传递额外的上下文：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">include</span> <span class="s2">"name_snippet.html"</span> <span class="k">with</span> <span class="nv">person</span><span class="o">=</span><span class="s2">"Jane"</span> <span class="nv">greeting</span><span class="o">=</span><span class="s2">"Hello"</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>如果你想只用所提供的变量（甚至不使用变量）来渲染上下文，请使用 <code class="docutils literal notranslate"><span class="pre">only</span></code> 选项。所包含的模板没有其他变量可用：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">include</span> <span class="s2">"name_snippet.html"</span> <span class="k">with</span> <span class="nv">greeting</span><span class="o">=</span><span class="s2">"Hi"</span> <span class="nv">only</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p><a class="reference internal" href="#std:templatetag-include"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">include</span></code></a> 标签应被视为 “渲染这个子模板并包含 HTML” 的实现，而不是 “解析这个子模板并包含其内容，就像它是父模板的一部分一样”。这意味着，包含的模板之间没有共享状态——每个包含都是一个完全独立的渲染过程。</p>
<p class="last">块在被包含之前会被执行。这意味着一个包含了另一个模板块的模板将包含 <em>已经被执行和渲染</em> 的块，而不是可以被覆盖的块，例如，一个扩展模板。</p>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 3.1:</span> <p>增加了对可迭代对象作为模板名称的支持。</p>
</div>
</div>
<div class="section" id="s-load">
<span id="s-std:templatetag-load"></span><span id="load"></span><span id="std:templatetag-load"></span><h3><code class="docutils literal notranslate"><span class="pre">load</span></code><a class="headerlink" href="#load" title="永久链接至标题">¶</a></h3>
<p>加载一个自定义模板标签集。</p>
<p>例如，下面的模板将加载所有在 <code class="docutils literal notranslate"><span class="pre">somelibrary</span></code> 和 <code class="docutils literal notranslate"><span class="pre">otherlibrary</span></code> 中注册的标签和过滤器，这些标签和过滤器位于 <code class="docutils literal notranslate"><span class="pre">package</span></code> ：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">somelibrary</span> <span class="nv">package.otherlibrary</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>你也可以使用 <code class="docutils literal notranslate"><span class="pre">from</span></code> 参数，有选择地从一个库中加载单个过滤器或标签。在这个例子中，名为 <code class="docutils literal notranslate"><span class="pre">foo</span></code> 和 <code class="docutils literal notranslate"><span class="pre">bar</span></code> 的模板标签／过滤器将从 <code class="docutils literal notranslate"><span class="pre">somelibrary</span></code> 加载：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">foo</span> <span class="nv">bar</span> <span class="nv">from</span> <span class="nv">somelibrary</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>更多信息请参见 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/howto/custom-template-tags/"><span class="doc">自定义标签和过滤器库</span></a>。</p>
</div>
<div class="section" id="s-lorem">
<span id="s-std:templatetag-lorem"></span><span id="lorem"></span><span id="std:templatetag-lorem"></span><h3><code class="docutils literal notranslate"><span class="pre">lorem</span></code><a class="headerlink" href="#lorem" title="永久链接至标题">¶</a></h3>
<p>显示随机的 “经验之谈” 拉丁文文本。这对于在模板中提供样本数据很有用。</p>
<p>用法：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">lorem</span> <span class="o">[</span><span class="nv">count</span><span class="o">]</span> <span class="o">[</span><span class="nv">method</span><span class="o">]</span> <span class="o">[</span><span class="nv">random</span><span class="o">]</span> <span class="cp">%}</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">lorem</span> <span class="pre">%}</span></code> 标签可以使用零、一、二或三个参数。这些参数是：</p>
<table class="docutils">
<colgroup>
<col width="15%">
<col width="85%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">参数</th>
<th class="head">描述</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">count</span></code></td>
<td>一个数字（或变量），包含要生成的段落或字数（默认为 1）。</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">method</span></code></td>
<td>字词为 <code class="docutils literal notranslate"><span class="pre">w</span></code>，HTML 段落为 <code class="docutils literal notranslate"><span class="pre">p</span></code>，纯文本段落块为 <code class="docutils literal notranslate"><span class="pre">b</span></code> （默认为 <code class="docutils literal notranslate"><span class="pre">b</span></code>）。</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">random</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">random</span></code> 一词，如果给定，在生成文本时不使用普通段落（“Lorem ipsum dolor sit amet.”）。</td>
</tr>
</tbody>
</table>
<p>举例：</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">lorem</span> <span class="pre">%}</span></code> 将输出常见的 “经验之谈” 段落。</li>
<li><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">lorem</span> <span class="pre">3</span> <span class="pre">p</span> <span class="pre">%}</span></code> 将输出普通的 “经验之谈” 段落和两个随机段落，每个段落用 HTML <code class="docutils literal notranslate"><span class="pre">&lt;p&gt;</span></code> 标签包装。</li>
<li><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">lorem</span> <span class="pre">2</span> <span class="pre">w</span> <span class="pre">random</span> <span class="pre">%}</span></code> 将随机输出两个拉丁语单词。</li>
</ul>
</div>
<div class="section" id="s-now">
<span id="s-std:templatetag-now"></span><span id="now"></span><span id="std:templatetag-now"></span><h3><code class="docutils literal notranslate"><span class="pre">now</span></code><a class="headerlink" href="#now" title="永久链接至标题">¶</a></h3>
<p>显示当前日期和／或时间，根据给定的字符串使用格式。这些字符串可以包含格式指定符，如 <a class="reference internal" href="#std:templatefilter-date"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">date</span></code></a> 过滤器部分所述。</p>
<p>举例：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span>It is <span class="cp">{%</span> <span class="k">now</span> <span class="s2">"jS F Y H:i"</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>请注意，如果你想使用 “原始” 值，你可以对格式字符串进行反斜杠转义。在这个例子中，“o” 和 “f” 都被反斜杠转义了，因为否则每个都是一个格式字符串，分别显示年份和时间：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span>It is the <span class="cp">{%</span> <span class="k">now</span> <span class="s2">"jS \o\f F"</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>这样就会显示为 “It is the 4th of September”。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p>传递的格式也可以是预定义的 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-DATE_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DATE_FORMAT</span></code></a>、<a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-DATETIME_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DATETIME_FORMAT</span></code></a>、<a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-SHORT_DATE_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">SHORT_DATE_FORMAT</span></code></a> 或 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-SHORT_DATETIME_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">SHORT_DATETIME_FORMAT</span></code></a>。预定义的格式可能会有所不同，这取决于当前的 locale 和是否启用了 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/topics/i18n/formatting/"><span class="doc">本地格式化</span></a>，例如：</p>
<div class="last highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span>It is <span class="cp">{%</span> <span class="k">now</span> <span class="s2">"SHORT_DATETIME_FORMAT"</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<p>你也可以使用 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">now</span> <span class="pre">"Y"</span> <span class="pre">as</span> <span class="pre">current_year</span> <span class="pre">%}</span></code> 的语法将输出（作为字符串）存储在一个变量中。如果你想在模板标签中使用 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">now</span> <span class="pre">%}</span></code>，比如 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/topics/i18n/translation/#std:templatetag-blocktranslate"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">blocktranslate</span></code></a>，这就很有用。</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">now</span> <span class="s2">"Y"</span> <span class="k">as</span> <span class="nv">current_year</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">blocktranslate</span> <span class="cp">%}</span>Copyright <span class="cp">{{</span> <span class="nv">current_year</span> <span class="cp">}}{%</span> <span class="k">endblocktranslate</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-regroup">
<span id="s-std:templatetag-regroup"></span><span id="regroup"></span><span id="std:templatetag-regroup"></span><h3><code class="docutils literal notranslate"><span class="pre">regroup</span></code><a class="headerlink" href="#regroup" title="永久链接至标题">¶</a></h3>
<p>通过一个共同的属性将一个相似的对象列表重新分组。</p>
<p>这个复杂的标签通过一个例子得到了最好的说明：假设 <code class="docutils literal notranslate"><span class="pre">cities</span></code> 是一个由包含 <code class="docutils literal notranslate"><span class="pre">"name"</span></code>、<code class="docutils literal notranslate"><span class="pre">"population"</span></code> 和 <code class="docutils literal notranslate"><span class="pre">"country"</span></code> 键的字典所代表的城市列表。</p>
<div class="highlight-python notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="n">cities</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'Mumbai'</span><span class="p">,</span> <span class="s1">'population'</span><span class="p">:</span> <span class="s1">'19,000,000'</span><span class="p">,</span> <span class="s1">'country'</span><span class="p">:</span> <span class="s1">'India'</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'Calcutta'</span><span class="p">,</span> <span class="s1">'population'</span><span class="p">:</span> <span class="s1">'15,000,000'</span><span class="p">,</span> <span class="s1">'country'</span><span class="p">:</span> <span class="s1">'India'</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'New York'</span><span class="p">,</span> <span class="s1">'population'</span><span class="p">:</span> <span class="s1">'20,000,000'</span><span class="p">,</span> <span class="s1">'country'</span><span class="p">:</span> <span class="s1">'USA'</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'Chicago'</span><span class="p">,</span> <span class="s1">'population'</span><span class="p">:</span> <span class="s1">'7,000,000'</span><span class="p">,</span> <span class="s1">'country'</span><span class="p">:</span> <span class="s1">'USA'</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'Tokyo'</span><span class="p">,</span> <span class="s1">'population'</span><span class="p">:</span> <span class="s1">'33,000,000'</span><span class="p">,</span> <span class="s1">'country'</span><span class="p">:</span> <span class="s1">'Japan'</span><span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>...而你想显示一个按国家排序的分层列表，像这样：</p>
<ul class="simple">
<li>India<ul>
<li>Mumbai: 19,000,000</li>
<li>Calcutta: 15,000,000</li>
</ul>
</li>
<li>USA<ul>
<li>New York: 20,000,000</li>
<li>Chicago: 7,000,000</li>
</ul>
</li>
<li>Japan<ul>
<li>Tokyo: 33,000,000</li>
</ul>
</li>
</ul>
<p>你可以使用 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></code> 标签按国家对城市列表进行分组。以下模板代码片段可以实现这一目的：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">regroup</span> <span class="nv">cities</span> <span class="nv">by</span> <span class="nv">country</span> <span class="k">as</span> <span class="nv">country_list</span> <span class="cp">%}</span>

<span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">country</span> <span class="k">in</span> <span class="nv">country_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">country.grouper</span> <span class="cp">}}</span>
    <span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
        <span class="cp">{%</span> <span class="k">for</span> <span class="nv">city</span> <span class="k">in</span> <span class="nv">country.list</span> <span class="cp">%}</span>
          <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">city.name</span> <span class="cp">}}</span>: <span class="cp">{{</span> <span class="nv">city.population</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
        <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
    <span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>让我们来看看这个例子。<code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></code> 需要三个参数：要重新分组的列表、要分组的属性和结果列表的名称。在这里，我们通过 <code class="docutils literal notranslate"><span class="pre">country</span></code> 属性对 <code class="docutils literal notranslate"><span class="pre">cities</span></code> 列表进行重新分组，并将结果称为 <code class="docutils literal notranslate"><span class="pre">country_list</span></code>。</p>
<p><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></code> 产生一个 <strong>组对象</strong> 的列表（在本例中为 <code class="docutils literal notranslate"><span class="pre">country_list</span></code>）。组对象是 <a class="reference external" href="https://docs.python.org/3/library/collections.html#collections.namedtuple" title="(在 Python v3.10)"><code class="xref py py-func docutils literal notranslate"><span class="pre">namedtuple()</span></code></a> 的实例，有两个字段。</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">grouper</span></code> —— 被分组的项目（例如，“India” 或 “Japan” 等字符串）。</li>
<li><code class="docutils literal notranslate"><span class="pre">list</span></code> —— 本组所有项目的清单（例如，country='India' 的所有城市的清单）。</li>
</ul>
<p>因为 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></code> 会产生 <a class="reference external" href="https://docs.python.org/3/library/collections.html#collections.namedtuple" title="(在 Python v3.10)"><code class="xref py py-func docutils literal notranslate"><span class="pre">namedtuple()</span></code></a> 对象，所以也可以把前面的例子写成：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">regroup</span> <span class="nv">cities</span> <span class="nv">by</span> <span class="nv">country</span> <span class="k">as</span> <span class="nv">country_list</span> <span class="cp">%}</span>

<span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">country</span><span class="o">,</span> <span class="nv">local_cities</span> <span class="k">in</span> <span class="nv">country_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">country</span> <span class="cp">}}</span>
    <span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
        <span class="cp">{%</span> <span class="k">for</span> <span class="nv">city</span> <span class="k">in</span> <span class="nv">local_cities</span> <span class="cp">%}</span>
          <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">city.name</span> <span class="cp">}}</span>: <span class="cp">{{</span> <span class="nv">city.population</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
        <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
    <span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>请注意，<code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></code> 并没有对其输入进行排序！我们的例子依赖于这样一个事实： <code class="docutils literal notranslate"><span class="pre">cities</span></code> 列表首先是按 <code class="docutils literal notranslate"><span class="pre">country</span></code> 排序的。如果 <code class="docutils literal notranslate"><span class="pre">cities</span></code> 列表没有按 <code class="docutils literal notranslate"><span class="pre">country</span></code> 排序，重新分组就会天真地显示一个国家的多个组。例如，如果 <code class="docutils literal notranslate"><span class="pre">cities</span></code> 列表设置成这样（注意，国家没有被归为一组）。</p>
<div class="highlight-python notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="n">cities</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'Mumbai'</span><span class="p">,</span> <span class="s1">'population'</span><span class="p">:</span> <span class="s1">'19,000,000'</span><span class="p">,</span> <span class="s1">'country'</span><span class="p">:</span> <span class="s1">'India'</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'New York'</span><span class="p">,</span> <span class="s1">'population'</span><span class="p">:</span> <span class="s1">'20,000,000'</span><span class="p">,</span> <span class="s1">'country'</span><span class="p">:</span> <span class="s1">'USA'</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'Calcutta'</span><span class="p">,</span> <span class="s1">'population'</span><span class="p">:</span> <span class="s1">'15,000,000'</span><span class="p">,</span> <span class="s1">'country'</span><span class="p">:</span> <span class="s1">'India'</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'Chicago'</span><span class="p">,</span> <span class="s1">'population'</span><span class="p">:</span> <span class="s1">'7,000,000'</span><span class="p">,</span> <span class="s1">'country'</span><span class="p">:</span> <span class="s1">'USA'</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'Tokyo'</span><span class="p">,</span> <span class="s1">'population'</span><span class="p">:</span> <span class="s1">'33,000,000'</span><span class="p">,</span> <span class="s1">'country'</span><span class="p">:</span> <span class="s1">'Japan'</span><span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>有了这个 <code class="docutils literal notranslate"><span class="pre">cities</span></code> 的输入，上面的 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">regroup</span> <span class="pre">%}</span></code> 模板代码就会有如下输出。</p>
<ul class="simple">
<li>India<ul>
<li>Mumbai: 19,000,000</li>
</ul>
</li>
<li>USA<ul>
<li>New York: 20,000,000</li>
</ul>
</li>
<li>India<ul>
<li>Calcutta: 15,000,000</li>
</ul>
</li>
<li>USA<ul>
<li>Chicago: 7,000,000</li>
</ul>
</li>
<li>Japan<ul>
<li>Tokyo: 33,000,000</li>
</ul>
</li>
</ul>
<p>解决这个问题最简单的方法是在你的视图代码中确保数据是按照你想显示的方式来排序的。</p>
<p>另一种解决方案是使用 <a class="reference internal" href="#std:templatefilter-dictsort"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">dictsort</span></code></a> 过滤器对模板中的数据进行排序，如果你的数据是在字典列表中：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">regroup</span> <span class="nv">cities</span><span class="o">|</span><span class="nf">dictsort</span><span class="s2">:"country"</span> <span class="nv">by</span> <span class="nv">country</span> <span class="k">as</span> <span class="nv">country_list</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="section" id="s-grouping-on-other-properties">
<span id="grouping-on-other-properties"></span><h4>对其他属性进行分组<a class="headerlink" href="#grouping-on-other-properties" title="永久链接至标题">¶</a></h4>
<p>任何有效的模板查找都是重新分组标签的合法分组属性，包括方法、属性、字典键和列表项。例如，如果 “country” 字段是一个具有 “description” 属性的类的外键，你可以使用：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">regroup</span> <span class="nv">cities</span> <span class="nv">by</span> <span class="nv">country.description</span> <span class="k">as</span> <span class="nv">country_list</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>或者，如果 <code class="docutils literal notranslate"><span class="pre">country</span></code> 是一个带有 <code class="docutils literal notranslate"><span class="pre">choices</span></code> 的字段，它将有一个 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/models/instances/#django.db.models.Model.get_FOO_display" title="django.db.models.Model.get_FOO_display"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_FOO_display()</span></code></a> 方法作为属性，允许你对显示字符串进行分组，而不是 <code class="docutils literal notranslate"><span class="pre">choices</span></code> 键：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">regroup</span> <span class="nv">cities</span> <span class="nv">by</span> <span class="nv">get_country_display</span> <span class="k">as</span> <span class="nv">country_list</span> <span class="cp">%}</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">country.grouper</span> <span class="pre">}}</span></code> 现在将显示 <code class="docutils literal notranslate"><span class="pre">choices</span></code> 集的值域，而不是键。</p>
</div>
</div>
<div class="section" id="s-resetcycle">
<span id="s-std:templatetag-resetcycle"></span><span id="resetcycle"></span><span id="std:templatetag-resetcycle"></span><h3><code class="docutils literal notranslate"><span class="pre">resetcycle</span></code><a class="headerlink" href="#resetcycle" title="永久链接至标题">¶</a></h3>
<p>重置之前的 <a class="reference internal" href="#cycle">cycle</a> ，使其在下一次遇到时从第一项开始重新启动。如果没有参数，<code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">resetcycle</span> <span class="pre">%}</span></code> 将重置模板中定义的最后一个 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">cycle</span> <span class="pre">%}</span></code>。</p>
<p>用法示例：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">coach</span> <span class="k">in</span> <span class="nv">coach_list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">coach.name</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">for</span> <span class="nv">athlete</span> <span class="k">in</span> <span class="nv">coach.athlete_set.all</span> <span class="cp">%}</span>
        <span class="p">&lt;</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'odd'</span> <span class="s1">'even'</span> <span class="cp">%}</span><span class="s">"</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">athlete.name</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">resetcycle</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>这个例子会返回这样的HTML：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>José Mourinho<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">"odd"</span><span class="p">&gt;</span>Thibaut Courtois<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">"even"</span><span class="p">&gt;</span>John Terry<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">"odd"</span><span class="p">&gt;</span>Eden Hazard<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>

<span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span>Carlo Ancelotti<span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">"odd"</span><span class="p">&gt;</span>Manuel Neuer<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">"even"</span><span class="p">&gt;</span>Thomas Müller<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>请注意第一个代码块以 <code class="docutils literal notranslate"><span class="pre">class="odd"</span></code> 结束，而新的代码块以 <code class="docutils literal notranslate"><span class="pre">class="odd"</span></code> 开始。如果没有 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">resetcycle</span> <span class="pre">%}</span></code> 标签，第二块将以 <code class="docutils literal notranslate"><span class="pre">class="even"</span></code> 开始。</p>
<p>你还可以重置命名的循环标签：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">list</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'odd'</span> <span class="s1">'even'</span> <span class="k">as</span> <span class="nv">stripe</span> <span class="cp">%}</span><span class="s"> </span><span class="cp">{%</span> <span class="k">cycle</span> <span class="s1">'major'</span> <span class="s1">'minor'</span> <span class="s1">'minor'</span> <span class="s1">'minor'</span> <span class="s1">'minor'</span> <span class="k">as</span> <span class="nv">tick</span> <span class="cp">%}</span><span class="s">"</span><span class="p">&gt;</span>
        <span class="cp">{{</span> <span class="nv">item.data</span> <span class="cp">}}</span>
    <span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
    <span class="cp">{%</span> <span class="k">ifchanged</span> <span class="nv">item.category</span> <span class="cp">%}</span>
        <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;</span><span class="cp">{{</span> <span class="nv">item.category</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span>
        <span class="cp">{%</span> <span class="k">if</span> <span class="k">not</span> <span class="nb">forloop</span><span class="nv">.first</span> <span class="cp">%}{%</span> <span class="k">resetcycle</span> <span class="nv">tick</span> <span class="cp">%}{%</span> <span class="k">endif</span> <span class="cp">%}</span>
    <span class="cp">{%</span> <span class="k">endifchanged</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>在这个例子中，我们既有奇数／偶数行的交替，也有每第五行的 “主要” 行。只有当类别发生变化时，才会重置五行循环。</p>
</div>
<div class="section" id="s-spaceless">
<span id="s-std:templatetag-spaceless"></span><span id="spaceless"></span><span id="std:templatetag-spaceless"></span><h3><code class="docutils literal notranslate"><span class="pre">spaceless</span></code><a class="headerlink" href="#spaceless" title="永久链接至标题">¶</a></h3>
<p>删除 HTML 标签之间的空白。这包括制表符和换行符。</p>
<p>用法示例：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">spaceless</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>
        <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"foo/"</span><span class="p">&gt;</span>Foo<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endspaceless</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>这个例子会返回这样的HTML：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"foo/"</span><span class="p">&gt;</span>Foo<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>只有 <em>标签</em> 之间的空格会被删除，而不是标签和文本之间的空格。在这个例子中，<code class="docutils literal notranslate"><span class="pre">Hello</span></code> 周围的空格不会被删除：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">spaceless</span> <span class="cp">%}</span>
    <span class="p">&lt;</span><span class="nt">strong</span><span class="p">&gt;</span>
        Hello
    <span class="p">&lt;/</span><span class="nt">strong</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endspaceless</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-templatetag">
<span id="s-std:templatetag-templatetag"></span><span id="templatetag"></span><span id="std:templatetag-templatetag"></span><h3><code class="docutils literal notranslate"><span class="pre">templatetag</span></code><a class="headerlink" href="#templatetag" title="永久链接至标题">¶</a></h3>
<p>输出用于组成模板标签的语法字符之一。</p>
<p>由于模板系统没有 “转义” 的概念，所以要显示模板标签中使用的一个位，必须使用 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">templatetag</span> <span class="pre">%}</span></code> 标签。</p>
<p>这个参数告诉我们要输出哪个模板位：</p>
<table class="docutils">
<colgroup>
<col width="72%">
<col width="28%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">参数</th>
<th class="head">输出</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">openblock</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{%</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">closeblock</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">%}</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">openvariable</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">closevariable</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">}}</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">openbrace</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">closebrace</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">}</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">opencomment</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{#</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">closecomment</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">#}</span></code></td>
</tr>
</tbody>
</table>
<p>示例用法：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">templatetag</span> <span class="nv">openblock</span> <span class="cp">%}</span> url 'entry_list' <span class="cp">{%</span> <span class="k">templatetag</span> <span class="nv">closeblock</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-url">
<span id="s-std:templatetag-url"></span><span id="url"></span><span id="std:templatetag-url"></span><h3><code class="docutils literal notranslate"><span class="pre">url</span></code><a class="headerlink" href="#url" title="永久链接至标题">¶</a></h3>
<p>返回与给定视图和可选参数相匹配的绝对路径引用（不含域名的 URL）。路径中的任何特殊字符将使用 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/utils/#django.utils.encoding.iri_to_uri" title="django.utils.encoding.iri_to_uri"><code class="xref py py-func docutils literal notranslate"><span class="pre">iri_to_uri()</span></code></a> 进行编码。</p>
<p>这是在不违反 DRY 原则的情况下，通过在模板中硬编码 URL 来输出链接的方法：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">'some-url-name'</span> <span class="nv">v1</span> <span class="nv">v2</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>第一个参数是一个 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/topics/http/urls/#naming-url-patterns"><span class="std std-ref">URL 模式名称</span></a>。它可以是一个引号文字或任何其他上下文变量。其他参数是可选的，应该是以空格分隔的值，这些值将在 URL 中作为参数使用。上面的例子显示的是传递位置参数。另外，你也可以使用关键字语法：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">'some-url-name'</span> <span class="nv">arg1</span><span class="o">=</span><span class="nv">v1</span> <span class="nv">arg2</span><span class="o">=</span><span class="nv">v2</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>不要在一次调用中同时混合使用位置和关键字语法。URLconf 所要求的所有参数都应该存在。</p>
<p>例如，假设你有一个视图，<code class="docutils literal notranslate"><span class="pre">app_views.client</span></code>，它的 URLconf 需要一个客户端 ID（这里，<code class="docutils literal notranslate"><span class="pre">client()</span></code> 是视图文件 <code class="docutils literal notranslate"><span class="pre">app_views.py</span></code> 中的一个方法）。URLconf 行可能是这样的：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="n">path</span><span class="p">(</span><span class="s1">'client/&lt;int:id&gt;/'</span><span class="p">,</span> <span class="n">app_views</span><span class="o">.</span><span class="n">client</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">'app-views-client'</span><span class="p">)</span>
</pre></div>
</div>
<p>如果这个应用的 URLconf 被包含在项目的 URLconf 中，路径如下：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="n">path</span><span class="p">(</span><span class="s1">'clients/'</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="s1">'project_name.app_name.urls'</span><span class="p">))</span>
</pre></div>
</div>
<p>...然后，在模板中，你可以像这样创建一个链接到这个视图：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">'app-views-client'</span> <span class="nv">client.id</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>模板标签将输出字符串 <code class="docutils literal notranslate"><span class="pre">/clients/client/123/</span></code>。</p>
<p>请注意，如果你要反查的 URL 不存在，你会得到一个 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/exceptions/#django.urls.NoReverseMatch" title="django.urls.NoReverseMatch"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NoReverseMatch</span></code></a> 的异常引发，这将导致你的网站显示一个错误页面。</p>
<p>如果你想在不显示的情况下检索一个 URL，你可以使用一个稍微不同的调用：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">'some-url-name'</span> <span class="nv">arg</span> <span class="nv">arg2</span> <span class="k">as</span> <span class="nv">the_url</span> <span class="cp">%}</span>

<span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">the_url</span> <span class="cp">}}</span><span class="s">"</span><span class="p">&gt;</span>I'm linking to <span class="cp">{{</span> <span class="nv">the_url</span> <span class="cp">}}</span><span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">as</span> <span class="pre">var</span></code> 语法创建的变量范围是 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></code>，其中出现 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">url</span> <span class="pre">%}</span></code> 标签。</p>
<p>如果缺少视图，这种 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">url</span> <span class="pre">...</span> <span class="pre">as</span> <span class="pre">var</span> <span class="pre">%}</span></code> 语法 <em>不会</em> 引起错误。在实践中，你会用它来链接到可选的视图：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">'some-url-name'</span> <span class="k">as</span> <span class="nv">the_url</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="nv">the_url</span> <span class="cp">%}</span>
  <span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">the_url</span> <span class="cp">}}</span><span class="s">"</span><span class="p">&gt;</span>Link to optional stuff<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>如果你想检索一个命名空间的 URL，请指定完全限定的名称：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">url</span> <span class="s1">'myapp:view-name'</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>这将遵循正常的 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/topics/http/urls/#topics-http-reversing-url-namespaces"><span class="std std-ref">命名空间 URL 解析策略</span></a>，包括使用上下文提供的关于当前应用程序的任何提示。</p>
<div class="admonition warning">
<p class="first admonition-title">警告</p>
<p class="last">不要忘记在 URL 模式的 <code class="docutils literal notranslate"><span class="pre">name</span></code> 周围加上引号，否则其值将被解释为一个上下文变量！</p>
</div>
</div>
<div class="section" id="s-verbatim">
<span id="s-std:templatetag-verbatim"></span><span id="verbatim"></span><span id="std:templatetag-verbatim"></span><h3><code class="docutils literal notranslate"><span class="pre">verbatim</span></code><a class="headerlink" href="#verbatim" title="永久链接至标题">¶</a></h3>
<p>停止模板引擎渲染此块标签的内容。</p>
<p>常见的用法是允许 JavaScript 模板层与 Django 的语法发生碰撞。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">verbatim</span> <span class="cp">%}</span>
    <span class="cp">{{</span><span class="k">if</span> <span class="nv">dying</span><span class="cp">}}</span>Still alive.<span class="cp">{{</span><span class="o">/</span><span class="k">if</span><span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endverbatim</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>你也可以指定一个特定的结束标签，允许使用 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">endverbatim</span> <span class="pre">%}</span></code> 作为未渲染内容的一部分：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">verbatim</span> <span class="nv">myblock</span> <span class="cp">%}</span>
    Avoid template rendering via the <span class="cp">{%</span> <span class="k">verbatim</span> <span class="cp">%}{%</span> <span class="k">endverbatim</span> <span class="cp">%}</span> block.
<span class="cp">{%</span> <span class="k">endverbatim</span> <span class="nv">myblock</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-widthratio">
<span id="s-std:templatetag-widthratio"></span><span id="widthratio"></span><span id="std:templatetag-widthratio"></span><h3><code class="docutils literal notranslate"><span class="pre">widthratio</span></code><a class="headerlink" href="#widthratio" title="永久链接至标题">¶</a></h3>
<p>对于创建条形图等，该标签计算给定值与最大值的比率，然后将该比率应用于一个常数。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">&lt;</span><span class="nt">img</span> <span class="na">src</span><span class="o">=</span><span class="s">"bar.png"</span> <span class="na">alt</span><span class="o">=</span><span class="s">"Bar"</span>
     <span class="na">height</span><span class="o">=</span><span class="s">"10"</span> <span class="na">width</span><span class="o">=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">widthratio</span> <span class="nv">this_value</span> <span class="nv">max_value</span> <span class="nv">max_width</span> <span class="cp">%}</span><span class="s">"</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">this_value</span></code> 是175，<code class="docutils literal notranslate"><span class="pre">max_value</span></code> 是 200，<code class="docutils literal notranslate"><span class="pre">max_width</span></code> 是 100，则上例中的图像宽度为 88 像素（因为 175 / 200 = 0.875；0.875 * 100 = 87.5，四舍五入为 88）。</p>
<p>在某些情况下，你可能想在一个变量中捕获 <code class="docutils literal notranslate"><span class="pre">widthratio</span></code> 的结果。例如，在 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/topics/i18n/translation/#std:templatetag-blocktranslate"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">blocktranslate</span></code></a> 中，它可能是有用的，像这样：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">widthratio</span> <span class="nv">this_value</span> <span class="nv">max_value</span> <span class="nv">max_width</span> <span class="k">as</span> <span class="nv">width</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">blocktranslate</span> <span class="cp">%}</span>The width is: <span class="cp">{{</span> <span class="nv">width</span> <span class="cp">}}{%</span> <span class="k">endblocktranslate</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-with">
<span id="s-std:templatetag-with"></span><span id="with"></span><span id="std:templatetag-with"></span><h3><code class="docutils literal notranslate"><span class="pre">with</span></code><a class="headerlink" href="#with" title="永久链接至标题">¶</a></h3>
<p>将一个复杂的变量缓存在一个简单的名称下。当多次访问一个 “昂贵的” 方法（例如，访问数据库的方法）时，这很有用。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">with</span> <span class="nv">total</span><span class="o">=</span><span class="nv">business.employees.count</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">total</span> <span class="cp">}}</span> employee<span class="cp">{{</span> <span class="nv">total</span><span class="o">|</span><span class="nf">pluralize</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endwith</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>被填充的变量（在上面的例子中，<code class="docutils literal notranslate"><span class="pre">total</span></code>）只有在 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">with</span> <span class="pre">%}</span></code> 和 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">endwith</span> <span class="pre">%}</span></code> 标签之间才能使用。</p>
<p>你可以指定一个以上的上下文变量：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">with</span> <span class="nv">alpha</span><span class="o">=</span><span class="m">1</span> <span class="nv">beta</span><span class="o">=</span><span class="m">2</span> <span class="cp">%}</span>
    ...
<span class="cp">{%</span> <span class="k">endwith</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">仍然支持以前比较啰嗦的格式： <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">with</span> <span class="pre">business.employees.count</span> <span class="pre">as</span> <span class="pre">total</span> <span class="pre">%}</span></code>。</p>
</div>
</div>
</div>
<div class="section" id="s-built-in-filter-reference">
<span id="s-ref-templates-builtins-filters"></span><span id="built-in-filter-reference"></span><span id="ref-templates-builtins-filters"></span><h2>内置过滤器参考<a class="headerlink" href="#built-in-filter-reference" title="永久链接至标题">¶</a></h2>
<div class="section" id="s-add">
<span id="s-std:templatefilter-add"></span><span id="add"></span><span id="std:templatefilter-add"></span><h3><code class="docutils literal notranslate"><span class="pre">add</span></code><a class="headerlink" href="#add" title="永久链接至标题">¶</a></h3>
<p>将参数添加到值中。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">add</span><span class="s2">:"2"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">4</span></code>，那么输出将是 <code class="docutils literal notranslate"><span class="pre">6</span></code>。</p>
<p>这个过滤器将首先尝试将两个值强制转为整数。如果失败了，它将尝试将两个值加在一起。这对某些数据类型（字符串、列表等）有效，而对其他类型则失败。如果失败，结果将是一个空字符串。</p>
<p>例如，如果我们有：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">first</span><span class="o">|</span><span class="nf">add</span><span class="o">:</span><span class="nv">second</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>同时 <code class="docutils literal notranslate"><span class="pre">first</span></code> 是 <code class="docutils literal notranslate"><span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3]</span></code> 并且 <code class="docutils literal notranslate"><span class="pre">second</span></code> 是 <code class="docutils literal notranslate"><span class="pre">[4,</span> <span class="pre">5,</span> <span class="pre">6]</span></code>，则输出为 <code class="docutils literal notranslate"><span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3,</span> <span class="pre">4,</span> <span class="pre">5,</span> <span class="pre">6]</span></code>。</p>
<div class="admonition warning">
<p class="first admonition-title">警告</p>
<p class="last">可以强制转为整数的字符串将被 <strong>相加</strong>，而不是像上面上一个例子那样被连在一起。</p>
</div>
</div>
<div class="section" id="s-addslashes">
<span id="s-std:templatefilter-addslashes"></span><span id="addslashes"></span><span id="std:templatefilter-addslashes"></span><h3><code class="docutils literal notranslate"><span class="pre">addslashes</span></code><a class="headerlink" href="#addslashes" title="永久链接至标题">¶</a></h3>
<p>在引号前添加斜杠。对 CSV 中的字符串进行转义很有用，例如。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">addslashes</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"I'm</span> <span class="pre">using</span> <span class="pre">Django"</span></code>，那么输出将是 <code class="docutils literal notranslate"><span class="pre">"I\'m</span> <span class="pre">using</span> <span class="pre">Django"</span></code>。</p>
</div>
<div class="section" id="s-capfirst">
<span id="s-std:templatefilter-capfirst"></span><span id="capfirst"></span><span id="std:templatefilter-capfirst"></span><h3><code class="docutils literal notranslate"><span class="pre">capfirst</span></code><a class="headerlink" href="#capfirst" title="永久链接至标题">¶</a></h3>
<p>将值的第一个字符大写。如果第一个字符不是字母，这个过滤器就没有效果。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">capfirst</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"django"</span></code>，则输出为 <code class="docutils literal notranslate"><span class="pre">"Django"</span></code>。</p>
</div>
<div class="section" id="s-center">
<span id="s-std:templatefilter-center"></span><span id="center"></span><span id="std:templatefilter-center"></span><h3><code class="docutils literal notranslate"><span class="pre">center</span></code><a class="headerlink" href="#center" title="永久链接至标题">¶</a></h3>
<p>在给定宽度的字段中使数值居中。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span>"<span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">center</span><span class="s2">:"15"</span> <span class="cp">}}</span>"
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"Django"</span></code>，输出将是 <code class="docutils literal notranslate"><span class="pre">"</span>&nbsp;&nbsp;&nbsp;&nbsp; <span class="pre">Django</span>&nbsp;&nbsp;&nbsp; <span class="pre">"</span></code>。</p>
</div>
<div class="section" id="s-cut">
<span id="s-std:templatefilter-cut"></span><span id="cut"></span><span id="std:templatefilter-cut"></span><h3><code class="docutils literal notranslate"><span class="pre">cut</span></code><a class="headerlink" href="#cut" title="永久链接至标题">¶</a></h3>
<p>从给定的字符串中删除参数的所有值。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">cut</span><span class="s2">:" "</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"String</span> <span class="pre">with</span> <span class="pre">spaces"</span></code>，输出将是 <code class="docutils literal notranslate"><span class="pre">"Stringwithspaces"</span></code>。</p>
</div>
<div class="section" id="s-date">
<span id="s-std:templatefilter-date"></span><span id="date"></span><span id="std:templatefilter-date"></span><h3><code class="docutils literal notranslate"><span class="pre">date</span></code><a class="headerlink" href="#date" title="永久链接至标题">¶</a></h3>
<p>根据给定的格式设置日期。</p>
<p>Uses a similar format to PHP's <a class="reference external" href="https://www.php.net/manual/en/function.date.php">date()</a> function with some
differences.</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">这些格式字符在模板之外的 Django 中没有使用。它们被设计成与 PHP 兼容，以方便设计师过渡。</p>
</div>
<p id="date-and-time-formatting-specifiers">可用的格式字符串：</p>
<table class="docutils">
<colgroup>
<col width="12%">
<col width="31%">
<col width="57%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">格式字符</th>
<th class="head">描述</th>
<th class="head">输出示例</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><strong>日</strong></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">d</span></code></td>
<td>某月的某日，2 位带前导零的数字。</td>
<td><code class="docutils literal notranslate"><span class="pre">'01'</span></code> 到 <code class="docutils literal notranslate"><span class="pre">'31'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">j</span></code></td>
<td>某月的某日，前面没有零。</td>
<td><code class="docutils literal notranslate"><span class="pre">'1'</span></code> 到 <code class="docutils literal notranslate"><span class="pre">'31'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">D</span></code></td>
<td>某周的某日，3 个字母的文本。</td>
<td><code class="docutils literal notranslate"><span class="pre">'Fri'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">l</span></code></td>
<td>某周的某日，长文本。</td>
<td><code class="docutils literal notranslate"><span class="pre">'Friday'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">S</span></code></td>
<td>某月英文序号后缀的某日，2 个字符。</td>
<td><code class="docutils literal notranslate"><span class="pre">'st'</span></code>、<code class="docutils literal notranslate"><span class="pre">'nd'</span></code>、<code class="docutils literal notranslate"><span class="pre">'rd'</span></code> 或 <code class="docutils literal notranslate"><span class="pre">'th'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">w</span></code></td>
<td>某周的某日，不含前导零的数字。</td>
<td><code class="docutils literal notranslate"><span class="pre">'0'</span></code> （星期天）到 <code class="docutils literal notranslate"><span class="pre">'6'</span></code> （星期六）</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">z</span></code></td>
<td>某年的某日</td>
<td><code class="docutils literal notranslate"><span class="pre">1</span></code> 到 <code class="docutils literal notranslate"><span class="pre">366</span></code></td>
</tr>
<tr class="row-even"><td><strong>周</strong></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">W</span></code></td>
<td>年的 ISO-8601 周数，周从星期一开始。</td>
<td><code class="docutils literal notranslate"><span class="pre">1</span></code>，<code class="docutils literal notranslate"><span class="pre">53</span></code></td>
</tr>
<tr class="row-even"><td><strong>月</strong></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">m</span></code></td>
<td>月份，2 位数，前面加 0。</td>
<td><code class="docutils literal notranslate"><span class="pre">'01'</span></code> 到 <code class="docutils literal notranslate"><span class="pre">'12'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">n</span></code></td>
<td>没有前导零的月份。</td>
<td><code class="docutils literal notranslate"><span class="pre">'1'</span></code> 到 <code class="docutils literal notranslate"><span class="pre">'12'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">M</span></code></td>
<td>月，3 个字母的文本。</td>
<td><code class="docutils literal notranslate"><span class="pre">'Jan'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">b</span></code></td>
<td>月，小写的 3 个字母的文本。</td>
<td><code class="docutils literal notranslate"><span class="pre">'jan'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">E</span></code></td>
<td>月，特定地域的替代表示方法，通常用于长日期表示。</td>
<td><code class="docutils literal notranslate"><span class="pre">'listopada'</span></code> （波兰语，与 <code class="docutils literal notranslate"><span class="pre">'Listopad'</span></code> 相对）</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">F</span></code></td>
<td>月，长文本。</td>
<td><code class="docutils literal notranslate"><span class="pre">'January'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">N</span></code></td>
<td>美联社风格的月份缩写。专属扩展。</td>
<td><code class="docutils literal notranslate"><span class="pre">'Jan.'</span></code>、<code class="docutils literal notranslate"><span class="pre">'Feb.'</span></code>、<code class="docutils literal notranslate"><span class="pre">'March'</span></code>、<code class="docutils literal notranslate"><span class="pre">'May'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">t</span></code></td>
<td>特定月份的天数。</td>
<td><code class="docutils literal notranslate"><span class="pre">28</span></code> 到 <code class="docutils literal notranslate"><span class="pre">31</span></code></td>
</tr>
<tr class="row-odd"><td><strong>年</strong></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">y</span></code></td>
<td>Year, 2 digits with leading zeros.</td>
<td><code class="docutils literal notranslate"><span class="pre">'00'</span></code> 到 <code class="docutils literal notranslate"><span class="pre">'99'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Y</span></code></td>
<td>年，4 位数字。</td>
<td><code class="docutils literal notranslate"><span class="pre">'1999'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">L</span></code></td>
<td>是否为闰年的布尔值。</td>
<td><code class="docutils literal notranslate"><span class="pre">True</span></code> 或 <code class="docutils literal notranslate"><span class="pre">False</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">o</span></code></td>
<td>ISO-8601 周数年，对应于使用闰周的 ISO-8601 周数（W）。更常见的年份格式见 Y。</td>
<td><code class="docutils literal notranslate"><span class="pre">'1999'</span></code></td>
</tr>
<tr class="row-even"><td><strong>时间</strong></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">g</span></code></td>
<td>小时，12 小时格式，无前导零。</td>
<td><code class="docutils literal notranslate"><span class="pre">'1'</span></code> 到 <code class="docutils literal notranslate"><span class="pre">'12'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">G</span></code></td>
<td>小时，24 小时格式，无前导零。</td>
<td><code class="docutils literal notranslate"><span class="pre">'0'</span></code> 到 <code class="docutils literal notranslate"><span class="pre">'23'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">h</span></code></td>
<td>小时，12 小时格式。</td>
<td><code class="docutils literal notranslate"><span class="pre">'01'</span></code> 到 <code class="docutils literal notranslate"><span class="pre">'12'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">H</span></code></td>
<td>小时，24 小时格式。</td>
<td><code class="docutils literal notranslate"><span class="pre">'00'</span></code> 到 <code class="docutils literal notranslate"><span class="pre">'23'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">i</span></code></td>
<td>分钟。</td>
<td><code class="docutils literal notranslate"><span class="pre">'00'</span></code> 到 <code class="docutils literal notranslate"><span class="pre">'59'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">s</span></code></td>
<td>秒，2 位带前导零的数字。</td>
<td><code class="docutils literal notranslate"><span class="pre">'00'</span></code> 到 <code class="docutils literal notranslate"><span class="pre">'59'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">u</span></code></td>
<td>微秒。</td>
<td><code class="docutils literal notranslate"><span class="pre">000000</span></code> 到 <code class="docutils literal notranslate"><span class="pre">999999</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">a</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">'a.m.'</span></code> 或 <code class="docutils literal notranslate"><span class="pre">'p.m.'</span></code> （请注意，这与 PHP 的输出略有不同，因为这包括了与美联社风格一致的句号）。</td>
<td><code class="docutils literal notranslate"><span class="pre">'a.m.'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">A</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">'AM'</span></code> 或 <code class="docutils literal notranslate"><span class="pre">'PM'</span></code>。</td>
<td><code class="docutils literal notranslate"><span class="pre">'AM'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">f</span></code></td>
<td>时间，以 12 小时格式的小时和分钟为单位，如果是零，就把分钟去掉。专属扩展。</td>
<td><code class="docutils literal notranslate"><span class="pre">'1'</span></code>，<code class="docutils literal notranslate"><span class="pre">'1:30'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">P</span></code></td>
<td>时间，以 12 小时格式的小时、分钟和  'a.m.'／'p.m.' 为单位，如果分钟为零，则不写，如果有，则写上特殊大小写的字符串 'midnight' 和 'noon'。专属扩展。</td>
<td><code class="docutils literal notranslate"><span class="pre">'1</span> <span class="pre">a.m.'</span></code>、<code class="docutils literal notranslate"><span class="pre">'1:30</span> <span class="pre">p.m.'</span></code>、<code class="docutils literal notranslate"><span class="pre">'midnight'</span></code>、<code class="docutils literal notranslate"><span class="pre">'noon'</span></code>、<code class="docutils literal notranslate"><span class="pre">'12:30</span> <span class="pre">p.m.'</span></code></td>
</tr>
<tr class="row-even"><td><strong>时区</strong></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">e</span></code></td>
<td>时区名称。可以是任何格式，也可以返回一个空字符串，取决于日期时间。</td>
<td><code class="docutils literal notranslate"><span class="pre">''</span></code>、<code class="docutils literal notranslate"><span class="pre">'GMT'</span></code>、<code class="docutils literal notranslate"><span class="pre">'-500'</span></code>、<code class="docutils literal notranslate"><span class="pre">'US/Eastern'</span></code> 等。</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">I</span></code></td>
<td>夏令时，无论是否生效。</td>
<td><code class="docutils literal notranslate"><span class="pre">'1'</span></code> 或 <code class="docutils literal notranslate"><span class="pre">'0'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">O</span></code></td>
<td>与格林威治时间的时差。</td>
<td><code class="docutils literal notranslate"><span class="pre">'+0200'</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">T</span></code></td>
<td>本机的时区。</td>
<td><code class="docutils literal notranslate"><span class="pre">'EST'</span></code>、<code class="docutils literal notranslate"><span class="pre">'MDT'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">Z</span></code></td>
<td>时区偏移以秒为单位。UTC 以西的时区偏移总是负数，UTC 以东的时区偏移总是正数。</td>
<td><code class="docutils literal notranslate"><span class="pre">-43200</span></code> 到 <code class="docutils literal notranslate"><span class="pre">43200</span></code></td>
</tr>
<tr class="row-even"><td><strong>日期／时间</strong></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">c</span></code></td>
<td>ISO 8601 format. (Note: unlike other
formatters, such as "Z", "O" or "r",
the "c" formatter will not add timezone
offset if value is a naive datetime
(see <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.tzinfo" title="(在 Python v3.10)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.tzinfo</span></code></a>).</td>
<td><code class="docutils literal notranslate"><span class="pre">2008-01-02T10:30:00.000123+02:00</span></code> 或 <code class="docutils literal notranslate"><span class="pre">2008-01-02T10:30:00.000123</span></code> 如果日期时间是本地的</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">r</span></code></td>
<td><span class="target" id="index-2"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc5322.html#section-3.3"><strong>RFC 5322</strong></a> 格式的日期。</td>
<td><code class="docutils literal notranslate"><span class="pre">'Thu,</span> <span class="pre">21</span> <span class="pre">Dec</span> <span class="pre">2000</span> <span class="pre">16:01:07</span> <span class="pre">+0200'</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">U</span></code></td>
<td>自 Unix 纪元（1970 年 1 月 1 日 00:00:00 UTC）以来的秒数。</td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"D d M Y"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是一个 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(在 Python v3.10)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 对象（例如，<code class="docutils literal notranslate"><span class="pre">datetime.datetime.datetime.now()</span></code> 的结果），输出将是字符串 <code class="docutils literal notranslate"><span class="pre">'Wed</span> <span class="pre">09</span> <span class="pre">Jan</span> <span class="pre">2008'</span></code>。</p>
<p>传递的格式可以是预定义的 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-DATE_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DATE_FORMAT</span></code></a>、<a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-DATETIME_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">DATETIME_FORMAT</span></code></a>、<a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-SHORT_DATE_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">SHORT_DATE_FORMAT</span></code></a> 或 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-SHORT_DATETIME_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">SHORT_DATETIME_FORMAT</span></code></a>, 或者是使用上表中显示的格式指定器的自定义格式。请注意，预定义的格式可能会根据当前的 locale 而有所不同。</p>
<p>假设 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-USE_L10N"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_L10N</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 和 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-LANGUAGE_CODE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LANGUAGE_CODE</span></code></a> 为例如 <code class="docutils literal notranslate"><span class="pre">"es"</span></code>，那么对于：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"SHORT_DATE_FORMAT"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>输出将是字符串 <code class="docutils literal notranslate"><span class="pre">"09/01/2008"</span></code> （Django 自带的 <code class="docutils literal notranslate"><span class="pre">"SHORT_DATE_FORMAT"</span></code> 格式指定符是 <code class="docutils literal notranslate"><span class="pre">"d/m/Y"</span></code>）。</p>
<p>当不使用格式字符串时，使用 <code class="docutils literal notranslate"><span class="pre">DATE_FORMAT</span></code> 格式指定器。假设设置与前一个例子相同：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">date</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>输出 <code class="docutils literal notranslate"><span class="pre">9</span> <span class="pre">de</span> <span class="pre">Enero</span> <span class="pre">de</span> <span class="pre">2008</span></code> （<code class="docutils literal notranslate"><span class="pre">DATE_FORMAT'</span></code> 格式指定符为 <code class="docutils literal notranslate"><span class="pre">r'j\d\e</span> <span class="pre">F\d\e</span> <span class="pre">Y'</span></code>）。“d” 和 “e” 都是反斜杠，因为否则每一个都是一个格式字符串，分别显示日期和时区名称。</p>
<p>你可以将 <code class="docutils literal notranslate"><span class="pre">date</span></code> 与 <a class="reference internal" href="#std:templatefilter-time"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">time</span></code></a> 过滤器结合起来，以呈现一个 <code class="docutils literal notranslate"><span class="pre">datetime</span></code> 值的完整表示。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">date</span><span class="s2">:"D d M Y"</span> <span class="cp">}}</span> <span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span><span class="s2">:"H:i"</span> <span class="cp">}}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-default">
<span id="s-std:templatefilter-default"></span><span id="default"></span><span id="std:templatefilter-default"></span><h3><code class="docutils literal notranslate"><span class="pre">default</span></code><a class="headerlink" href="#default" title="永久链接至标题">¶</a></h3>
<p>如果值为 <code class="docutils literal notranslate"><span class="pre">False</span></code>，则使用给定的默认值。否则，使用该值。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">default</span><span class="s2">:"nothing"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">""</span></code> （空字符串），输出将是 <code class="docutils literal notranslate"><span class="pre">nothing</span></code>。</p>
</div>
<div class="section" id="s-default-if-none">
<span id="s-std:templatefilter-default_if_none"></span><span id="default-if-none"></span><span id="std:templatefilter-default_if_none"></span><h3><code class="docutils literal notranslate"><span class="pre">default_if_none</span></code><a class="headerlink" href="#default-if-none" title="永久链接至标题">¶</a></h3>
<p>如果（也只有当）值是 <code class="docutils literal notranslate"><span class="pre">None</span></code>，使用给定的默认值。否则，使用该值。</p>
<p>请注意，如果给定一个空字符串，将 <em>不会</em> 使用默认值。如果你想使用空字符串，请使用 <a class="reference internal" href="#std:templatefilter-default"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">default</span></code></a> 过滤器。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">default_if_none</span><span class="s2">:"nothing"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">None</span></code>，则输出为 <code class="docutils literal notranslate"><span class="pre">nothing</span></code>。</p>
</div>
<div class="section" id="s-dictsort">
<span id="s-std:templatefilter-dictsort"></span><span id="dictsort"></span><span id="std:templatefilter-dictsort"></span><h3><code class="docutils literal notranslate"><span class="pre">dictsort</span></code><a class="headerlink" href="#dictsort" title="永久链接至标题">¶</a></h3>
<p>获取一个字典列表，并按照参数中给出的键排序返回该列表。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">dictsort</span><span class="s2">:"name"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">[</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'zed'</span><span class="p">,</span> <span class="s1">'age'</span><span class="p">:</span> <span class="mi">19</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'amy'</span><span class="p">,</span> <span class="s1">'age'</span><span class="p">:</span> <span class="mi">22</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'joe'</span><span class="p">,</span> <span class="s1">'age'</span><span class="p">:</span> <span class="mi">31</span><span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>那么输出将是：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">[</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'amy'</span><span class="p">,</span> <span class="s1">'age'</span><span class="p">:</span> <span class="mi">22</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'joe'</span><span class="p">,</span> <span class="s1">'age'</span><span class="p">:</span> <span class="mi">31</span><span class="p">},</span>
    <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'zed'</span><span class="p">,</span> <span class="s1">'age'</span><span class="p">:</span> <span class="mi">19</span><span class="p">},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>你还可以做更复杂的事情，比如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">book</span> <span class="k">in</span> <span class="nv">books</span><span class="o">|</span><span class="nf">dictsort</span><span class="s2">:"author.age"</span> <span class="cp">%}</span>
    * <span class="cp">{{</span> <span class="nv">book.title</span> <span class="cp">}}</span> (<span class="cp">{{</span> <span class="nv">book.author.name</span> <span class="cp">}}</span>)
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">books</span></code> 是：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">[</span>
    <span class="p">{</span><span class="s1">'title'</span><span class="p">:</span> <span class="s1">'1984'</span><span class="p">,</span> <span class="s1">'author'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'George'</span><span class="p">,</span> <span class="s1">'age'</span><span class="p">:</span> <span class="mi">45</span><span class="p">}},</span>
    <span class="p">{</span><span class="s1">'title'</span><span class="p">:</span> <span class="s1">'Timequake'</span><span class="p">,</span> <span class="s1">'author'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'Kurt'</span><span class="p">,</span> <span class="s1">'age'</span><span class="p">:</span> <span class="mi">75</span><span class="p">}},</span>
    <span class="p">{</span><span class="s1">'title'</span><span class="p">:</span> <span class="s1">'Alice'</span><span class="p">,</span> <span class="s1">'author'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'name'</span><span class="p">:</span> <span class="s1">'Lewis'</span><span class="p">,</span> <span class="s1">'age'</span><span class="p">:</span> <span class="mi">33</span><span class="p">}},</span>
<span class="p">]</span>
</pre></div>
</div>
<p>那么输出将是：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span>* Alice (Lewis)
* 1984 (George)
* Timequake (Kurt)
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">dictsort</span></code> 也可以按照指定的索引对列表（或任何其他实现 <code class="docutils literal notranslate"><span class="pre">__getitem__()</span></code> 的对象）进行排序。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">dictsort</span><span class="o">:</span><span class="m">0</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">[</span>
    <span class="p">(</span><span class="s1">'a'</span><span class="p">,</span> <span class="s1">'42'</span><span class="p">),</span>
    <span class="p">(</span><span class="s1">'c'</span><span class="p">,</span> <span class="s1">'string'</span><span class="p">),</span>
    <span class="p">(</span><span class="s1">'b'</span><span class="p">,</span> <span class="s1">'foo'</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>那么输出将是：</p>
<div class="highlight-python notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">[</span>
    <span class="p">(</span><span class="s1">'a'</span><span class="p">,</span> <span class="s1">'42'</span><span class="p">),</span>
    <span class="p">(</span><span class="s1">'b'</span><span class="p">,</span> <span class="s1">'foo'</span><span class="p">),</span>
    <span class="p">(</span><span class="s1">'c'</span><span class="p">,</span> <span class="s1">'string'</span><span class="p">),</span>
<span class="p">]</span>
</pre></div>
</div>
<p>你必须以整数而不是字符串的形式传递索引。以下情况会产生空的输出：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">values</span><span class="o">|</span><span class="nf">dictsort</span><span class="s2">:"0"</span> <span class="cp">}}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-dictsortreversed">
<span id="s-std:templatefilter-dictsortreversed"></span><span id="dictsortreversed"></span><span id="std:templatefilter-dictsortreversed"></span><h3><code class="docutils literal notranslate"><span class="pre">dictsortreversed</span></code><a class="headerlink" href="#dictsortreversed" title="永久链接至标题">¶</a></h3>
<p>取一个字典列表，并按参数中给出的键反向排序返回该列表。这与上面的过滤器的工作原理完全相同，但返回的值将是倒序的。</p>
</div>
<div class="section" id="s-divisibleby">
<span id="s-std:templatefilter-divisibleby"></span><span id="divisibleby"></span><span id="std:templatefilter-divisibleby"></span><h3><code class="docutils literal notranslate"><span class="pre">divisibleby</span></code><a class="headerlink" href="#divisibleby" title="永久链接至标题">¶</a></h3>
<p>如果数值被参数整除，则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">divisibleby</span><span class="s2">:"3"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">21</span></code>，则输出为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
</div>
<div class="section" id="s-escape">
<span id="s-std:templatefilter-escape"></span><span id="escape"></span><span id="std:templatefilter-escape"></span><h3><code class="docutils literal notranslate"><span class="pre">escape</span></code><a class="headerlink" href="#escape" title="永久链接至标题">¶</a></h3>
<p>转义字符串的 HTML。具体来说，它可以进行这些替换。</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">&lt;</span></code> 被替换为 <code class="docutils literal notranslate"><span class="pre">&amp;lt;</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">&gt;</span></code> 被替换为 <code class="docutils literal notranslate"><span class="pre">&amp;gt;</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'</span></code> （单引号）被替换为 <code class="docutils literal notranslate"><span class="pre">&amp;#x27;</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">"</span></code> （双引号）被替换为 <code class="docutils literal notranslate"><span class="pre">&amp;quot;</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">&amp;</span></code> 被替换为 <code class="docutils literal notranslate"><span class="pre">&amp;amp;</span></code></li>
</ul>
<p>将 <code class="docutils literal notranslate"><span class="pre">escape</span></code> 应用到一个通常会对结果进行自动转义的变量上，结果只会进行一轮转义。所以即使在自动转义环境中使用这个函数也是安全的。如果你想进行多次转义，请使用 <a class="reference internal" href="#std:templatefilter-force_escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">force_escape</span></code></a> 过滤器。</p>
<p>例如，当 <a href="#id1"><span class="problematic" id="id2">:tag:`autoescape`</span></a> 关闭时，你可以对字段应用 <code class="docutils literal notranslate"><span class="pre">escape</span></code> ：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">autoescape</span> <span class="nv">off</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">title</span><span class="o">|</span><span class="nf">escape</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-escapejs">
<span id="s-std:templatefilter-escapejs"></span><span id="escapejs"></span><span id="std:templatefilter-escapejs"></span><h3><code class="docutils literal notranslate"><span class="pre">escapejs</span></code><a class="headerlink" href="#escapejs" title="永久链接至标题">¶</a></h3>
<p>在 JavaScript 字符串中使用的转义字符。这并不能使字符串安全地用于 HTML 或 JavaScript 模板字元，但可以保护你在使用模板生成 JavaScript／JSON 时避免语法错误。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">escapejs</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"testing\r\njavascript</span> <span class="pre">'string\"</span> <span class="pre">&lt;b&gt;</span> <span class="pre">escaping</span> <span class="pre">&lt;/b&gt;"</span></code>，则输出为 <code class="docutils literal notranslate"><span class="pre">"testing\\u000D\\u000Ajavascript</span> <span class="pre">\\u0027string\\u0022</span> <span class="pre">\\u003Cb\\u003Eescaping\\u003C/b\\u003E"</span></code>。</p>
</div>
<div class="section" id="s-filesizeformat">
<span id="s-std:templatefilter-filesizeformat"></span><span id="filesizeformat"></span><span id="std:templatefilter-filesizeformat"></span><h3><code class="docutils literal notranslate"><span class="pre">filesizeformat</span></code><a class="headerlink" href="#filesizeformat" title="永久链接至标题">¶</a></h3>
<p>以 “人类可读” 的文件大小为格式（如 <code class="docutils literal notranslate"><span class="pre">'13</span> <span class="pre">KB'</span></code>、<code class="docutils literal notranslate"><span class="pre">'4.1</span> <span class="pre">MB'</span></code>、<code class="docutils literal notranslate"><span class="pre">'102</span> <span class="pre">bytes'</span></code> 等）。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">filesizeformat</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 123456789，则输出为 <code class="docutils literal notranslate"><span class="pre">117.7</span> <span class="pre">MB</span></code>。</p>
<div class="admonition-file-sizes-and-si-units admonition">
<p class="first admonition-title">文件大小和 SI 单位</p>
<p class="last">严格来说，<code class="docutils literal notranslate"><span class="pre">filesizeformat</span></code> 并不符合国际单位制，国际单位制建议在字节大小以 1024 的幂计算时使用 KiB、MiB、GiB 等单位（这里就是这种情况）。相反，Django 使用了传统的单位名称（KB、MB、GB 等），对应的是比较常用的名称。</p>
</div>
</div>
<div class="section" id="s-first">
<span id="s-std:templatefilter-first"></span><span id="first"></span><span id="std:templatefilter-first"></span><h3><code class="docutils literal notranslate"><span class="pre">first</span></code><a class="headerlink" href="#first" title="永久链接至标题">¶</a></h3>
<p>返回列表中的第一个项目。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">first</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是列表 <code class="docutils literal notranslate"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span></code>，则输出为 <code class="docutils literal notranslate"><span class="pre">'a'</span></code>。</p>
</div>
<div class="section" id="s-floatformat">
<span id="s-std:templatefilter-floatformat"></span><span id="floatformat"></span><span id="std:templatefilter-floatformat"></span><h3><code class="docutils literal notranslate"><span class="pre">floatformat</span></code><a class="headerlink" href="#floatformat" title="永久链接至标题">¶</a></h3>
<p>当不使用参数时，将浮点数四舍五入到小数点后一位——但只在有小数部分要显示的情况下。例如：</p>
<table class="docutils">
<colgroup>
<col width="26%">
<col width="57%">
<col width="17%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><code class="docutils literal notranslate"><span class="pre">value</span></code></th>
<th class="head">模板</th>
<th class="head">输出</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34.23234</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34.2</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">34.00000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34.26000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34.3</span></code></td>
</tr>
</tbody>
</table>
<p>如果与数字整数参数一起使用，<code class="docutils literal notranslate"><span class="pre">floatform</span></code> 将一个数字四舍五入到小数点后几位。例如：</p>
<table class="docutils">
<colgroup>
<col width="24%">
<col width="57%">
<col width="20%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><code class="docutils literal notranslate"><span class="pre">value</span></code></th>
<th class="head">模板</th>
<th class="head">输出</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34.23234</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:3</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34.232</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">34.00000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:3</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34.000</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34.26000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:3</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34.260</span></code></td>
</tr>
</tbody>
</table>
<p>特别有用的是传递 0（零）作为参数，它将把浮点数舍入到最接近的整数。</p>
<table class="docutils">
<colgroup>
<col width="22%">
<col width="59%">
<col width="19%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><code class="docutils literal notranslate"><span class="pre">value</span></code></th>
<th class="head">模板</th>
<th class="head">输出</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34.23234</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:"0"</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">34.00000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:"0"</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">39.56000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:"0"</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">40</span></code></td>
</tr>
</tbody>
</table>
<p>如果传递给 <code class="docutils literal notranslate"><span class="pre">floatform</span></code> 的参数是负数，它将把一个数字四舍五入到小数点后的位数——但只在有小数部分要显示的情况下。例如：</p>
<table class="docutils">
<colgroup>
<col width="22%">
<col width="59%">
<col width="19%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><code class="docutils literal notranslate"><span class="pre">value</span></code></th>
<th class="head">模板</th>
<th class="head">输出</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34.23234</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:"-3"</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34.232</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">34.00000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:"-3"</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34.26000</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:"-3"</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34.260</span></code></td>
</tr>
</tbody>
</table>
<p>If the argument passed to <code class="docutils literal notranslate"><span class="pre">floatformat</span></code> has the <code class="docutils literal notranslate"><span class="pre">g</span></code> suffix, it will force
grouping by the <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-THOUSAND_SEPARATOR"><code class="xref std std-setting docutils literal notranslate"><span class="pre">THOUSAND_SEPARATOR</span></code></a> for the active locale. For
example, when the active locale is <code class="docutils literal notranslate"><span class="pre">en</span></code> (English):</p>
<table class="docutils">
<colgroup>
<col width="21%">
<col width="57%">
<col width="22%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><code class="docutils literal notranslate"><span class="pre">value</span></code></th>
<th class="head">模板</th>
<th class="head">输出</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34232.34</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:"2g"</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34,232.34</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">34232.06</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:"g"</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34,232.1</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">34232.00</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">value|floatformat:"-3g"</span> <span class="pre">}}</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">34,232</span></code></td>
</tr>
</tbody>
</table>
<p>在没有参数的情况下使用 <code class="docutils literal notranslate"><span class="pre">floatformat</span></code> 相当于在参数为 <code class="docutils literal notranslate"><span class="pre">-1</span></code> 的情况下使用 <code class="docutils literal notranslate"><span class="pre">floatformat</span></code>。</p>
<div class="versionchanged">
<span class="title">Changed in Django 3.1:</span> <p>在旧版本中，对于四舍五入为零的负数，会返回一个负零 <code class="docutils literal notranslate"><span class="pre">-0</span></code>。</p>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 3.2:</span> <p>The <code class="docutils literal notranslate"><span class="pre">g</span></code> suffix to force grouping by thousand separators was added.</p>
</div>
</div>
<div class="section" id="s-force-escape">
<span id="s-std:templatefilter-force_escape"></span><span id="force-escape"></span><span id="std:templatefilter-force_escape"></span><h3><code class="docutils literal notranslate"><span class="pre">force_escape</span></code><a class="headerlink" href="#force-escape" title="永久链接至标题">¶</a></h3>
<p>对字符串进行 HTML 转义处理（详情请参见 <a class="reference internal" href="#std:templatefilter-escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escape</span></code></a> 过滤器）。这个过滤器会 <em>立即</em> 应用，并返回一个新的转义字符串。在极少数情况下，当你需要多次转义或想对转义结果应用其他过滤器时，这很有用。通常情况下，你要使用 <a class="reference internal" href="#std:templatefilter-escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escape</span></code></a> 过滤器。</p>
<p>例如，如果你想捕捉由 <a class="reference internal" href="#std:templatefilter-linebreaks"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">linebreaks</span></code></a> 过滤器创建的 <code class="docutils literal notranslate"><span class="pre">&lt;p&gt;</span></code> HTML 元素：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">autoescape</span> <span class="nv">off</span> <span class="cp">%}</span>
    <span class="cp">{{</span> <span class="nv">body</span><span class="o">|</span><span class="nf">linebreaks</span><span class="o">|</span><span class="nf">force_escape</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-get-digit">
<span id="s-std:templatefilter-get_digit"></span><span id="get-digit"></span><span id="std:templatefilter-get_digit"></span><h3><code class="docutils literal notranslate"><span class="pre">get_digit</span></code><a class="headerlink" href="#get-digit" title="永久链接至标题">¶</a></h3>
<p>给定一个整数，返回要求的数字，其中 1 是最右边的数字，2 是最右边的数字，等等。对于无效的输入（如果输入或参数不是整数，或者参数小于 1），返回原始值。否则，输出总是一个整数。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">get_digit</span><span class="s2">:"2"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">123456789</span></code>，则输出为 <code class="docutils literal notranslate"><span class="pre">8</span></code>。</p>
</div>
<div class="section" id="s-iriencode">
<span id="s-std:templatefilter-iriencode"></span><span id="iriencode"></span><span id="std:templatefilter-iriencode"></span><h3><code class="docutils literal notranslate"><span class="pre">iriencode</span></code><a class="headerlink" href="#iriencode" title="永久链接至标题">¶</a></h3>
<p>将 IRI（国际化资源标识符）转换为适合包含在 URL 中的字符串。如果你想在 URL 中使用包含非 ASCII 字符的字符串，这一点是必要的。</p>
<p>在已经通过 <a class="reference internal" href="#std:templatefilter-urlencode"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">urlencode</span></code></a> 过滤器的字符串上使用这个过滤器是安全的。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">iriencode</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"?test=1&amp;me=2"</span></code>，则输出为 <code class="docutils literal notranslate"><span class="pre">"?test=1&amp;amp;me=2"</span></code>。</p>
</div>
<div class="section" id="s-join">
<span id="s-std:templatefilter-join"></span><span id="join"></span><span id="std:templatefilter-join"></span><h3><code class="docutils literal notranslate"><span class="pre">join</span></code><a class="headerlink" href="#join" title="永久链接至标题">¶</a></h3>
<p>用字符串连接一个列表，就像 Python 的 <code class="docutils literal notranslate"><span class="pre">str.join(list)</span></code> 一样。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">join</span><span class="s2">:" // "</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是列表 <code class="docutils literal notranslate"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span></code>，输出将是字符串 <code class="docutils literal notranslate"><span class="pre">"a</span> <span class="pre">//</span> <span class="pre">b</span> <span class="pre">//</span> <span class="pre">c"</span></code>。</p>
</div>
<div class="section" id="s-json-script">
<span id="s-std:templatefilter-json_script"></span><span id="json-script"></span><span id="std:templatefilter-json_script"></span><h3><code class="docutils literal notranslate"><span class="pre">json_script</span></code><a class="headerlink" href="#json-script" title="永久链接至标题">¶</a></h3>
<p>安全地将 Python 对象输出为 JSON，用 <code class="docutils literal notranslate"><span class="pre">&lt;script&gt;</span></code> 标签包装，准备好与 JavaScript 一起使用。</p>
<p><strong>参数：</strong> HTML <code class="docutils literal notranslate"><span class="pre">&lt;script&gt;</span></code> 标签的 “id”。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">json_script</span><span class="s2">:"hello-data"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是字典 <code class="docutils literal notranslate"><span class="pre">{'hello':'world'}</span></code>，则输出为：</p>
<div class="highlight-html notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">&lt;</span><span class="nt">script</span> <span class="na">id</span><span class="o">=</span><span class="s">"hello-data"</span> <span class="na">type</span><span class="o">=</span><span class="s">"application/json"</span><span class="p">&gt;{</span><span class="s2">"hello"</span><span class="o">:</span> <span class="s2">"world"</span><span class="p">}&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>由此产生的数据可以用 JavaScript 来访问，比如这样：</p>
<div class="highlight-javascript notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="kd">const</span> <span class="nx">value</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="s1">'hello-data'</span><span class="p">).</span><span class="nx">textContent</span><span class="p">);</span>
</pre></div>
</div>
<p>XSS 攻击可以通过转义字符 “&lt;”、“&gt;” 和 “&amp;” 来缓解。例如，如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">{'hello':</span> <span class="pre">'world&lt;/script&gt;&amp;amp;'}</span></code>，则输出为：</p>
<div class="highlight-html notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">&lt;</span><span class="nt">script</span> <span class="na">id</span><span class="o">=</span><span class="s">"hello-data"</span> <span class="na">type</span><span class="o">=</span><span class="s">"application/json"</span><span class="p">&gt;{</span><span class="s2">"hello"</span><span class="o">:</span> <span class="s2">"world\\u003C/script\\u003E\\u0026amp;"</span><span class="p">}&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>这与严格的内容安全策略兼容，禁止在页面中执行脚本。它还保持了被动数据和可执行代码之间的明确分离。</p>
</div>
<div class="section" id="s-last">
<span id="s-std:templatefilter-last"></span><span id="last"></span><span id="std:templatefilter-last"></span><h3><code class="docutils literal notranslate"><span class="pre">last</span></code><a class="headerlink" href="#last" title="永久链接至标题">¶</a></h3>
<p>返回列表中的最后一项。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">last</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是列表 <code class="docutils literal notranslate"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c',</span> <span class="pre">'d']</span></code>，输出将是字符串 <code class="docutils literal notranslate"><span class="pre">"d"</span></code>。</p>
</div>
<div class="section" id="s-length">
<span id="s-std:templatefilter-length"></span><span id="length"></span><span id="std:templatefilter-length"></span><h3><code class="docutils literal notranslate"><span class="pre">length</span></code><a class="headerlink" href="#length" title="永久链接至标题">¶</a></h3>
<p>返回值的长度。这对字符串和列表都有效。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">length</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c',</span> <span class="pre">'d']</span></code> 或 <code class="docutils literal notranslate"><span class="pre">"abcd"</span></code>，输出将是 <code class="docutils literal notranslate"><span class="pre">4</span></code>。</p>
<p>过滤器对未定义的变量返回 <code class="docutils literal notranslate"><span class="pre">0</span></code>。</p>
</div>
<div class="section" id="s-length-is">
<span id="s-std:templatefilter-length_is"></span><span id="length-is"></span><span id="std:templatefilter-length_is"></span><h3><code class="docutils literal notranslate"><span class="pre">length_is</span></code><a class="headerlink" href="#length-is" title="永久链接至标题">¶</a></h3>
<p>如果值的长度是参数，则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>，否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">length_is</span><span class="s2">:"4"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c',</span> <span class="pre">'d']</span></code> 或 <code class="docutils literal notranslate"><span class="pre">"abcd"</span></code>，输出将是 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
</div>
<div class="section" id="s-linebreaks">
<span id="s-std:templatefilter-linebreaks"></span><span id="linebreaks"></span><span id="std:templatefilter-linebreaks"></span><h3><code class="docutils literal notranslate"><span class="pre">linebreaks</span></code><a class="headerlink" href="#linebreaks" title="永久链接至标题">¶</a></h3>
<p>用适当的 HTML 替换纯文本中的换行符；一个新行成为 HTML 换行符（<code class="docutils literal notranslate"><span class="pre">&lt;br&gt;</span></code>），一个新行后的空行成为段落换行符（<code class="docutils literal notranslate"><span class="pre">&lt;/p&gt;</span></code>）。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">linebreaks</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">Joel\nis</span> <span class="pre">a</span> <span class="pre">slug</span></code>，则输出为 <code class="docutils literal notranslate"><span class="pre">&lt;p&gt;Joel&lt;br&gt;is</span> <span class="pre">a</span> <span class="pre">slug&lt;/p&gt;</span></code>。</p>
</div>
<div class="section" id="s-linebreaksbr">
<span id="s-std:templatefilter-linebreaksbr"></span><span id="linebreaksbr"></span><span id="std:templatefilter-linebreaksbr"></span><h3><code class="docutils literal notranslate"><span class="pre">linebreaksbr</span></code><a class="headerlink" href="#linebreaksbr" title="永久链接至标题">¶</a></h3>
<p>将一段纯文本中的所有换行符转换为 HTML 换行符（<code class="docutils literal notranslate"><span class="pre">&lt;br&gt;</span></code>）。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">linebreaksbr</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">Joel/nis</span> <span class="pre">a</span> <span class="pre">slug</span></code>，则输出为 <code class="docutils literal notranslate"><span class="pre">Joel&lt;br&gt;is</span> <span class="pre">a</span> <span class="pre">slug</span></code>。</p>
</div>
<div class="section" id="s-linenumbers">
<span id="s-std:templatefilter-linenumbers"></span><span id="linenumbers"></span><span id="std:templatefilter-linenumbers"></span><h3><code class="docutils literal notranslate"><span class="pre">linenumbers</span></code><a class="headerlink" href="#linenumbers" title="永久链接至标题">¶</a></h3>
<p>显示带有行号的文本。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">linenumbers</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span>one
two
three
</pre></div>
</div>
<p>输出将是：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span>1. one
2. two
3. three
</pre></div>
</div>
</div>
<div class="section" id="s-ljust">
<span id="s-std:templatefilter-ljust"></span><span id="ljust"></span><span id="std:templatefilter-ljust"></span><h3><code class="docutils literal notranslate"><span class="pre">ljust</span></code><a class="headerlink" href="#ljust" title="永久链接至标题">¶</a></h3>
<p>左对齐给定宽度的字段中的值。</p>
<p><strong>参数：</strong> 字段大小</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span>"<span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">ljust</span><span class="s2">:"10"</span> <span class="cp">}}</span>"
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">Django</span></code>，则输出为 <code class="docutils literal notranslate"><span class="pre">"Django</span>&nbsp;&nbsp;&nbsp; <span class="pre">"</span></code>。</p>
</div>
<div class="section" id="s-lower">
<span id="s-std:templatefilter-lower"></span><span id="lower"></span><span id="std:templatefilter-lower"></span><h3><code class="docutils literal notranslate"><span class="pre">lower</span></code><a class="headerlink" href="#lower" title="永久链接至标题">¶</a></h3>
<p>将一个字符串转换为全小写。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">lower</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">Totally</span> <span class="pre">LOVING</span> <span class="pre">this</span> <span class="pre">Album!</span></code>，则输出为 <code class="docutils literal notranslate"><span class="pre">totally</span> <span class="pre">loving</span> <span class="pre">this</span> <span class="pre">album!</span></code>。</p>
</div>
<div class="section" id="s-make-list">
<span id="s-std:templatefilter-make_list"></span><span id="make-list"></span><span id="std:templatefilter-make_list"></span><h3><code class="docutils literal notranslate"><span class="pre">make_list</span></code><a class="headerlink" href="#make-list" title="永久链接至标题">¶</a></h3>
<p>返回变成列表的值。对于字符串，它是一个字符列表。对于整数来说，在创建列表之前，参数会被转换为一个字符串。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">make_list</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是字符串 <code class="docutils literal notranslate"><span class="pre">"Joel"</span></code>，输出将是列表 <code class="docutils literal notranslate"><span class="pre">['J',</span> <span class="pre">'o',</span> <span class="pre">'e',</span> <span class="pre">'l']</span></code>。如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">123</span></code>，输出将是列表 <code class="docutils literal notranslate"><span class="pre">['1'，'2'，'3']</span></code>。</p>
</div>
<div class="section" id="s-phone2numeric">
<span id="s-std:templatefilter-phone2numeric"></span><span id="phone2numeric"></span><span id="std:templatefilter-phone2numeric"></span><h3><code class="docutils literal notranslate"><span class="pre">phone2numeric</span></code><a class="headerlink" href="#phone2numeric" title="永久链接至标题">¶</a></h3>
<p>将一个电话号码（可能包含字母）转换为其数字等价物。</p>
<p>输入的不一定是有效的电话号码。这将很乐意转换任何字符串。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">phone2numeric</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">800-COLLECT</span></code>，则输出为 <code class="docutils literal notranslate"><span class="pre">800-2655328</span></code>。</p>
</div>
<div class="section" id="s-pluralize">
<span id="s-std:templatefilter-pluralize"></span><span id="pluralize"></span><span id="std:templatefilter-pluralize"></span><h3><code class="docutils literal notranslate"><span class="pre">pluralize</span></code><a class="headerlink" href="#pluralize" title="永久链接至标题">¶</a></h3>
<p>如果值不是 <code class="docutils literal notranslate"><span class="pre">1</span></code>、<code class="docutils literal notranslate"><span class="pre">'1'</span></code> 或长度为 1 的对象，则返回复数后缀。 默认情况下，后缀为 <code class="docutils literal notranslate"><span class="pre">'s'</span></code>。</p>
<p>举例：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span>You have <span class="cp">{{</span> <span class="nv">num_messages</span> <span class="cp">}}</span> message<span class="cp">{{</span> <span class="nv">num_messages</span><span class="o">|</span><span class="nf">pluralize</span> <span class="cp">}}</span>.
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">num_messages</span></code> 是 <code class="docutils literal notranslate"><span class="pre">1</span></code>，输出将是 <code class="docutils literal notranslate"><span class="pre">You</span> <span class="pre">have</span> <span class="pre">1</span> <span class="pre">message.</span></code> 如果 <code class="docutils literal notranslate"><span class="pre">num_messages</span></code> 是 <code class="docutils literal notranslate"><span class="pre">2</span></code>，输出将是 <code class="docutils literal notranslate"><span class="pre">You</span> <span class="pre">have</span> <span class="pre">2</span> <span class="pre">messages.</span></code>。</p>
<p>对于需要后缀而不是 <code class="docutils literal notranslate"><span class="pre">'s'</span></code> 的词，你可以提供一个备用后缀作为过滤器的参数。</p>
<p>举例：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span>You have <span class="cp">{{</span> <span class="nv">num_walruses</span> <span class="cp">}}</span> walrus<span class="cp">{{</span> <span class="nv">num_walruses</span><span class="o">|</span><span class="nf">pluralize</span><span class="s2">:"es"</span> <span class="cp">}}</span>.
</pre></div>
</div>
<p>对于不使用简单后缀进行复数的单词，你可以同时指定单数和复数后缀，并用逗号分隔。</p>
<p>举例：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span>You have <span class="cp">{{</span> <span class="nv">num_cherries</span> <span class="cp">}}</span> cherr<span class="cp">{{</span> <span class="nv">num_cherries</span><span class="o">|</span><span class="nf">pluralize</span><span class="s2">:"y,ies"</span> <span class="cp">}}</span>.
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">使用 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/topics/i18n/translation/#std:templatetag-blocktranslate"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">blocktranslate</span></code></a> 来实现翻译字符串的复数化。</p>
</div>
</div>
<div class="section" id="s-pprint">
<span id="s-std:templatefilter-pprint"></span><span id="pprint"></span><span id="std:templatefilter-pprint"></span><h3><code class="docutils literal notranslate"><span class="pre">pprint</span></code><a class="headerlink" href="#pprint" title="永久链接至标题">¶</a></h3>
<p><a class="reference external" href="https://docs.python.org/3/library/pprint.html#pprint.pprint" title="(在 Python v3.10)"><code class="xref py py-func docutils literal notranslate"><span class="pre">pprint.pprint()</span></code></a> 的一个包装器 —— 真的是用来调试的。</p>
</div>
<div class="section" id="s-random">
<span id="s-std:templatefilter-random"></span><span id="random"></span><span id="std:templatefilter-random"></span><h3><code class="docutils literal notranslate"><span class="pre">random</span></code><a class="headerlink" href="#random" title="永久链接至标题">¶</a></h3>
<p>从给定列表中随机返回一个项目。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">random</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是列表 <code class="docutils literal notranslate"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c',</span> <span class="pre">'d']</span></code>，输出可能是 <code class="docutils literal notranslate"><span class="pre">"b"</span></code>。</p>
</div>
<div class="section" id="s-rjust">
<span id="s-std:templatefilter-rjust"></span><span id="rjust"></span><span id="std:templatefilter-rjust"></span><h3><code class="docutils literal notranslate"><span class="pre">rjust</span></code><a class="headerlink" href="#rjust" title="永久链接至标题">¶</a></h3>
<p>右对齐给定宽度的字段中的值。</p>
<p><strong>参数：</strong> 字段大小</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span>"<span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">rjust</span><span class="s2">:"10"</span> <span class="cp">}}</span>"
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">Django</span></code>，输出将是 <code class="docutils literal notranslate"><span class="pre">"</span>&nbsp;&nbsp;&nbsp; <span class="pre">Django"</span></code>。</p>
</div>
<div class="section" id="s-safe">
<span id="s-std:templatefilter-safe"></span><span id="safe"></span><span id="std:templatefilter-safe"></span><h3><code class="docutils literal notranslate"><span class="pre">safe</span></code><a class="headerlink" href="#safe" title="永久链接至标题">¶</a></h3>
<p>标记一个字符串在输出前不需要进一步的 HTML 转义。当自动转义关闭时，该过滤器没有效果。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p>如果你是链式过滤器，在 <code class="docutils literal notranslate"><span class="pre">safe</span></code> 之后应用的过滤器会使内容再次不安全。例如，下面的代码按原样打印变量，未转义：</p>
<div class="last highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">var</span><span class="o">|</span><span class="nf">safe</span><span class="o">|</span><span class="nf">escape</span> <span class="cp">}}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-safeseq">
<span id="s-std:templatefilter-safeseq"></span><span id="safeseq"></span><span id="std:templatefilter-safeseq"></span><h3><code class="docutils literal notranslate"><span class="pre">safeseq</span></code><a class="headerlink" href="#safeseq" title="永久链接至标题">¶</a></h3>
<p>将 <a class="reference internal" href="#std:templatefilter-safe"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">safe</span></code></a> 过滤器应用于序列的每个元素。与其他对序列进行操作的过滤器，如 <a class="reference internal" href="#std:templatefilter-join"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">join</span></code></a> 一起使用。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">some_list</span><span class="o">|</span><span class="nf">safeseq</span><span class="o">|</span><span class="nf">join</span><span class="s2">:", "</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>在这种情况下，你不能直接使用 <a class="reference internal" href="#std:templatefilter-safe"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">safe</span></code></a> 过滤器，因为它会首先将变量转换为字符串，而不是处理序列中的单个元素。</p>
</div>
<div class="section" id="s-slice">
<span id="s-std:templatefilter-slice"></span><span id="slice"></span><span id="std:templatefilter-slice"></span><h3><code class="docutils literal notranslate"><span class="pre">slice</span></code><a class="headerlink" href="#slice" title="永久链接至标题">¶</a></h3>
<p>返回列表的一个片段。</p>
<p>Uses the same syntax as Python's list slicing. See
<a class="reference external" href="https://diveinto.org/python3/native-datatypes.html#slicinglists">https://diveinto.org/python3/native-datatypes.html#slicinglists</a> for an
introduction.</p>
<p>举例：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">some_list</span><span class="o">|</span><span class="nf">slice</span><span class="s2">:":2"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">some_list</span></code> 是 <code class="docutils literal notranslate"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span></code>，输出将是 <code class="docutils literal notranslate"><span class="pre">['a',</span> <span class="pre">'b']</span></code>。</p>
</div>
<div class="section" id="s-slugify">
<span id="s-std:templatefilter-slugify"></span><span id="slugify"></span><span id="std:templatefilter-slugify"></span><h3><code class="docutils literal notranslate"><span class="pre">slugify</span></code><a class="headerlink" href="#slugify" title="永久链接至标题">¶</a></h3>
<p>转换为 ASCII 码。将空格转换为连字符。移除非字母数字、下划线或连字符的字符。转换为小写字母。还可以去除前导和尾部的空白。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">slugify</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></code>，那么输出将是 <code class="docutils literal notranslate"><span class="pre">"joel-is-a-slug"</span></code>。</p>
</div>
<div class="section" id="s-stringformat">
<span id="s-std:templatefilter-stringformat"></span><span id="stringformat"></span><span id="std:templatefilter-stringformat"></span><h3><code class="docutils literal notranslate"><span class="pre">stringformat</span></code><a class="headerlink" href="#stringformat" title="永久链接至标题">¶</a></h3>
<p>根据参数——字符串格式化指定器，对变量进行格式化。这个指定符使用 <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#old-string-formatting" title="(在 Python v3.10)"><span>printf-style String Formatting</span></a> 语法，例外的是前面的 “%” 被删除。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">stringformat</span><span class="s2">:"E"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">10</span></code>，则输出为 <code class="docutils literal notranslate"><span class="pre">1.000000E+01</span></code>。</p>
</div>
<div class="section" id="s-striptags">
<span id="s-std:templatefilter-striptags"></span><span id="striptags"></span><span id="std:templatefilter-striptags"></span><h3><code class="docutils literal notranslate"><span class="pre">striptags</span></code><a class="headerlink" href="#striptags" title="永久链接至标题">¶</a></h3>
<p>尽一切努力剥离所有 [X]HTML 标签。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">striptags</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 为 <code class="docutils literal notranslate"><span class="pre">"&lt;b&gt;Joel&lt;/b&gt;</span> <span class="pre">&lt;button&gt;is&lt;/button&gt;</span> <span class="pre">a</span> <span class="pre">&lt;span&gt;slug&lt;/span&gt;"</span></code>, 那么输出就会是 <code class="docutils literal notranslate"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></code>.</p>
<div class="admonition-no-safety-guarantee admonition">
<p class="first admonition-title">没有安全保证</p>
<p class="last">请注意，<code class="docutils literal notranslate"><span class="pre">striptags</span></code> 并不能保证它的输出是 HTML 安全的，特别是对于非有效的 HTML 输入。所以 <strong>永远不要</strong> 应用 <code class="docutils literal notranslate"><span class="pre">safe</span></code> 过滤器到 <code class="docutils literal notranslate"><span class="pre">striptags</span></code> 的输出。如果你正在寻找更强大的东西，你可以使用 <code class="docutils literal notranslate"><span class="pre">bleach</span></code> Python 库，特别是它的 <a class="reference external" href="https://bleach.readthedocs.io/en/latest/clean.html">clean</a> 方法。</p>
</div>
</div>
<div class="section" id="s-time">
<span id="s-std:templatefilter-time"></span><span id="time"></span><span id="std:templatefilter-time"></span><h3><code class="docutils literal notranslate"><span class="pre">time</span></code><a class="headerlink" href="#time" title="永久链接至标题">¶</a></h3>
<p>根据给定的格式对时间进行格式化。</p>
<p>给定的格式可以是预定义的 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-TIME_FORMAT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">TIME_FORMAT</span></code></a>，也可以是自定义的格式，和 <a class="reference internal" href="#std:templatefilter-date"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">date</span></code></a> 过滤器一样。请注意，预定义的格式是依赖于 locale 的。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span><span class="s2">:"H:i"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 相当于 <code class="docutils literal notranslate"><span class="pre">datetime.datetime.now()</span></code>，输出将是字符串 <code class="docutils literal notranslate"><span class="pre">"01:23"</span></code>。</p>
<p>请注意，如果你想使用 “原始” 值，你可以对格式字符串进行反斜杠转义。在这个例子中，“h” 和 “m” 都被反斜杠转义了，因为如果不这样做，每一个都是一个格式字符串，分别显示小时和月份：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">value</span><span class="o">|</span><span class="nf">time</span><span class="s2">:"H\h i\m"</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>这样就会显示为 “01h 23m”。</p>
<p>另一个例子：</p>
<p>假设 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-USE_L10N"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_L10N</span></code></a> 为 <code class="docutils literal notranslate"><span class="pre">True</span></code> 和 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-LANGUAGE_CODE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">LANGUAGE_CODE</span></code></a> 为例如 <code class="docutils literal notranslate"><span class="pre">"de"</span></code>，那么对于：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span><span class="s2">:"TIME_FORMAT"</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>输出将是字符串 <code class="docutils literal notranslate"><span class="pre">"01:23"</span></code> （Django 自带的 <code class="docutils literal notranslate"><span class="pre">"TIME_FORMAT"</span></code> 格式指定符为 <code class="docutils literal notranslate"><span class="pre">"H:i"</span></code>）。</p>
<p><code class="docutils literal notranslate"><span class="pre">time</span></code> 过滤器只接受格式字符串中与一天中的时间有关的参数，而不是日期。如果你需要格式化一个 <code class="docutils literal notranslate"><span class="pre">date</span></code> 值，请使用 <a class="reference internal" href="#std:templatefilter-date"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">date</span></code></a> 过滤器来代替（如果你需要渲染一个完整的 <a class="reference external" href="https://docs.python.org/3/library/datetime.html#datetime.datetime" title="(在 Python v3.10)"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> 值，则使用 <a class="reference internal" href="#std:templatefilter-time"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">time</span></code></a>）。</p>
<p>上述规则有一个例外。当传递一个附带时区信息的 <code class="docutils literal notranslate"><span class="pre">datetime</span></code> 值时（一个 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/topics/i18n/timezones/#naive-vs-aware-datetimes"><span class="std std-ref">时区感知</span></a> 的 <code class="docutils literal notranslate"><span class="pre">datetime</span></code> 实例），<code class="docutils literal notranslate"><span class="pre">time</span></code> 过滤器将接受与时区相关的 <a class="reference internal" href="#date-and-time-formatting-specifiers"><span class="std std-ref">格式指定符</span></a> <code class="docutils literal notranslate"><span class="pre">'e'</span></code>、<code class="docutils literal notranslate"><span class="pre">'O'</span></code>、<code class="docutils literal notranslate"><span class="pre">'T'</span></code> 和 <code class="docutils literal notranslate"><span class="pre">'Z'</span></code>。</p>
<p>当没有使用格式字符串时，使用 <code class="docutils literal notranslate"><span class="pre">TIME_FORMAT</span></code> 格式指定符：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>同为：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">time</span><span class="s2">:"TIME_FORMAT"</span> <span class="cp">}}</span>
</pre></div>
</div>
</div>
<div class="section" id="s-timesince">
<span id="s-std:templatefilter-timesince"></span><span id="timesince"></span><span id="std:templatefilter-timesince"></span><h3><code class="docutils literal notranslate"><span class="pre">timesince</span></code><a class="headerlink" href="#timesince" title="永久链接至标题">¶</a></h3>
<p>将日期格式化为自该日期起的时间（如 “4 days, 6 hours”）。</p>
<p>取一个可选参数，该参数是一个变量，包含作为比较点的日期（如果没有该参数，比较点是 <em>现在</em> ）。例如，如果 <code class="docutils literal notranslate"><span class="pre">blog_date</span></code> 是代表 2006 年 6 月 1 日午夜的日期实例，<code class="docutils literal notranslate"><span class="pre">comment_date</span></code> 是 2006 年 6 月 1 日 08:00 的日期实例，那么下面将返回 “8 hours”：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">blog_date</span><span class="o">|</span><span class="nf">timesince</span><span class="o">:</span><span class="nv">comment_date</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>比较本地偏移和感知偏移的日期会返回一个空字符串。</p>
<p>分钟是使用的最小单位，凡是相对于比较点来说是未来的日期，都会返回 “0 minutes”。</p>
</div>
<div class="section" id="s-timeuntil">
<span id="s-std:templatefilter-timeuntil"></span><span id="timeuntil"></span><span id="std:templatefilter-timeuntil"></span><h3><code class="docutils literal notranslate"><span class="pre">timeuntil</span></code><a class="headerlink" href="#timeuntil" title="永久链接至标题">¶</a></h3>
<p>与 <code class="docutils literal notranslate"><span class="pre">timesince</span></code> 类似，不同的是，它测量的是从现在到给定日期或日期时间的时间。例如，如果今天是 2006 年 6 月 1 日，而 <code class="docutils literal notranslate"><span class="pre">conference_date</span></code> 是一个持有 2006 年 6 月 29 日的日期实例，那么 <code class="docutils literal notranslate"><span class="pre">{conference_date|timeuntil</span> <span class="pre">}}</span></code> 将返回 “4 weeks”。</p>
<p>取一个可选参数，该参数是一个包含日期的变量，用来作为比较点（而不是 <em>现在</em>）。如果 <code class="docutils literal notranslate"><span class="pre">from_date</span></code> 包含 2006 年 6 月 22 日，那么下面将返回 “1 week”：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">conference_date</span><span class="o">|</span><span class="nf">timeuntil</span><span class="o">:</span><span class="nv">from_date</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>比较本地偏移和感知偏移的日期会返回一个空字符串。</p>
<p>分钟是使用的最小单位，相对于比较点而言，任何处于过去的日期都将返回 “0 minutes”。</p>
</div>
<div class="section" id="s-title">
<span id="s-std:templatefilter-title"></span><span id="title"></span><span id="std:templatefilter-title"></span><h3><code class="docutils literal notranslate"><span class="pre">title</span></code><a class="headerlink" href="#title" title="永久链接至标题">¶</a></h3>
<p>通过使单词以大写字母开头，其余字符以小写字母开头，将字符串转换为大写字母。这个标签不会努力让 “琐碎的单词” 保持小写。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">title</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"my</span> <span class="pre">FIRST</span> <span class="pre">post"</span></code>，则输出将是 <code class="docutils literal notranslate"><span class="pre">"My</span> <span class="pre">First</span> <span class="pre">Post"</span></code>。</p>
</div>
<div class="section" id="s-truncatechars">
<span id="s-std:templatefilter-truncatechars"></span><span id="truncatechars"></span><span id="std:templatefilter-truncatechars"></span><h3><code class="docutils literal notranslate"><span class="pre">truncatechars</span></code><a class="headerlink" href="#truncatechars" title="永久链接至标题">¶</a></h3>
<p>如果一个字符串的长度超过指定的字符数，则截断它。截断后的字符串将以一个可翻译的省略号（“...”）结束。</p>
<p><strong>参数：</strong> 要截断的字符数</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">truncatechars</span><span class="o">:</span><span class="m">7</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></code>，则输出将是 <code class="docutils literal notranslate"><span class="pre">"Joel</span> <span class="pre">i…"</span></code>。</p>
</div>
<div class="section" id="s-truncatechars-html">
<span id="s-std:templatefilter-truncatechars_html"></span><span id="truncatechars-html"></span><span id="std:templatefilter-truncatechars_html"></span><h3><code class="docutils literal notranslate"><span class="pre">truncatechars_html</span></code><a class="headerlink" href="#truncatechars-html" title="永久链接至标题">¶</a></h3>
<p>类似于 <a class="reference internal" href="#std:templatefilter-truncatechars"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">truncatechars</span></code></a>，只是它能识别 HTML 标签。任何在字符串中打开但在截断点之前没有关闭的标签都会在截断后立即关闭。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">truncatechars_html</span><span class="o">:</span><span class="m">7</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"&lt;p&gt;Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug&lt;/p&gt;"</span></code>，则输出将是 <code class="docutils literal notranslate"><span class="pre">"&lt;p&gt;Joel</span> <span class="pre">i…&lt;/p&gt;"</span></code>。</p>
<p>HTML 内容中的新行将被保留。</p>
</div>
<div class="section" id="s-truncatewords">
<span id="s-std:templatefilter-truncatewords"></span><span id="truncatewords"></span><span id="std:templatefilter-truncatewords"></span><h3><code class="docutils literal notranslate"><span class="pre">truncatewords</span></code><a class="headerlink" href="#truncatewords" title="永久链接至标题">¶</a></h3>
<p>在一定字数后截断字符串。</p>
<p><strong>参数：</strong> 要在之后截断的字数</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">truncatewords</span><span class="o">:</span><span class="m">2</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></code>，则输出将是 <code class="docutils literal notranslate"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">…"</span></code>。</p>
<p>字符串内的新行将被删除。</p>
</div>
<div class="section" id="s-truncatewords-html">
<span id="s-std:templatefilter-truncatewords_html"></span><span id="truncatewords-html"></span><span id="std:templatefilter-truncatewords_html"></span><h3><code class="docutils literal notranslate"><span class="pre">truncatewords_html</span></code><a class="headerlink" href="#truncatewords-html" title="永久链接至标题">¶</a></h3>
<p>类似于 <a class="reference internal" href="#std:templatefilter-truncatewords"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">truncatewords</span></code></a>，只是它能识别 HTML 标签。任何在字符串中打开的标签，如果在截断点之前没有关闭，则会在截断后立即关闭。</p>
<p>这比 <a class="reference internal" href="#std:templatefilter-truncatewords"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">truncatewords</span></code></a> 效率低，所以只能在传递 HTML 文本时使用。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">truncatewords_html</span><span class="o">:</span><span class="m">2</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"&lt;p&gt;Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug&lt;/p&gt;"</span></code>，则输出将是 <code class="docutils literal notranslate"><span class="pre">"&lt;p&gt;Joel</span> <span class="pre">is</span> <span class="pre">…&lt;/p&gt;"</span></code>。</p>
<p>HTML 内容中的新行将被保留。</p>
</div>
<div class="section" id="s-unordered-list">
<span id="s-std:templatefilter-unordered_list"></span><span id="unordered-list"></span><span id="std:templatefilter-unordered_list"></span><h3><code class="docutils literal notranslate"><span class="pre">unordered_list</span></code><a class="headerlink" href="#unordered-list" title="永久链接至标题">¶</a></h3>
<p>递归地接受一个自嵌套列表，并返回一个 HTML 无序列表—— <strong>没有</strong> 打开和关闭 &lt;ul&gt; 标签。</p>
<p>假定列表的格式正确。例如，如果 <code class="docutils literal notranslate"><span class="pre">var</span></code> 包含 <code class="docutils literal notranslate"><span class="pre">['States',</span> <span class="pre">['Kansas',</span> <span class="pre">['Lawrence',</span> <span class="pre">'Topeka'],</span> <span class="pre">'Illinois']]</span></code>，那么 <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">var|unordered_list</span> <span class="pre">}}</span></code> 将返回：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>States
<span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
        <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>Kansas
        <span class="p">&lt;</span><span class="nt">ul</span><span class="p">&gt;</span>
                <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>Lawrence<span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
                <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>Topeka<span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
        <span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
        <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
        <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;</span>Illinois<span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">ul</span><span class="p">&gt;</span>
<span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-upper">
<span id="s-std:templatefilter-upper"></span><span id="upper"></span><span id="std:templatefilter-upper"></span><h3><code class="docutils literal notranslate"><span class="pre">upper</span></code><a class="headerlink" href="#upper" title="永久链接至标题">¶</a></h3>
<p>将一个字符串转换为全大写。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">upper</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></code>，则输出将是 <code class="docutils literal notranslate"><span class="pre">"JOEL</span> <span class="pre">IS</span> <span class="pre">A</span> <span class="pre">SLUG"</span></code>。</p>
</div>
<div class="section" id="s-urlencode">
<span id="s-std:templatefilter-urlencode"></span><span id="urlencode"></span><span id="std:templatefilter-urlencode"></span><h3><code class="docutils literal notranslate"><span class="pre">urlencode</span></code><a class="headerlink" href="#urlencode" title="永久链接至标题">¶</a></h3>
<p>转义一个值用于 URL。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">urlencode</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"https://www.example.org/foo?a=b&amp;c=d"</span></code>，则输出将是 <code class="docutils literal notranslate"><span class="pre">"https%3A//www.example.org/foo%3Fa%3Db%26c%3Dd"</span></code>。</p>
<p>可以提供一个可选的参数，包含不应该被转义的字符。</p>
<p>如果没有提供，则假定 '/' 字符是安全的。当 <em>所有</em> 字符都应该被转义时，可以提供一个空字符串。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">urlencode</span><span class="s2">:""</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"https://www.example.org/"</span></code>，则输出将是 <code class="docutils literal notranslate"><span class="pre">"https%3A%2F%2Fwww.example.org%2F"</span></code>。</p>
</div>
<div class="section" id="s-urlize">
<span id="s-std:templatefilter-urlize"></span><span id="urlize"></span><span id="std:templatefilter-urlize"></span><h3><code class="docutils literal notranslate"><span class="pre">urlize</span></code><a class="headerlink" href="#urlize" title="永久链接至标题">¶</a></h3>
<p>将文本中的 URL 和电子邮件地址转换为可点击的链接。</p>
<p>这个模板标签适用于以 <code class="docutils literal notranslate"><span class="pre">http://`、``https://</span></code> 或 <code class="docutils literal notranslate"><span class="pre">www.</span></code> 为前缀的链接。例如，<code class="docutils literal notranslate"><span class="pre">https://goo.gl/aia1t</span></code> 会被转换，但 <code class="docutils literal notranslate"><span class="pre">goo.gl/aia1t</span></code> 不会。</p>
<p>它还支持以原始顶级域（<code class="docutils literal notranslate"><span class="pre">.com</span></code>、<code class="docutils literal notranslate"><span class="pre">.edu</span></code>、<code class="docutils literal notranslate"><span class="pre">.gov</span></code>、<code class="docutils literal notranslate"><span class="pre">.int</span></code>、<code class="docutils literal notranslate"><span class="pre">.mil</span></code>、<code class="docutils literal notranslate"><span class="pre">.net</span></code> 和 <code class="docutils literal notranslate"><span class="pre">.org</span></code>）之一结尾的纯域链接。例如，<code class="docutils literal notranslate"><span class="pre">djangoproject.com</span></code> 被转换。</p>
<p>链接可以有尾部的标点符号（句号、逗号、闭括号），也可以有前面的标点符号（开头的小括号），<code class="docutils literal notranslate"><span class="pre">urlize</span></code> 仍然会做正确的事情。</p>
<p><code class="docutils literal notranslate"><span class="pre">urlize</span></code> 产生的链接有一个 <code class="docutils literal notranslate"><span class="pre">rel="nofollow"</span></code> 属性。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">urlize</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"Check</span> <span class="pre">out</span> <span class="pre">www.djangoproject.com"</span></code>，则输出将是 <code class="docutils literal notranslate"><span class="pre">"Check</span> <span class="pre">out</span> <span class="pre">&lt;a</span> <span class="pre">href="http://www.djangoproject.com"</span> <span class="pre">rel="nofollow"&gt;www.djangoproject.com&lt;/a&gt;"</span></code>。</p>
<p>除了网页链接，<code class="docutils literal notranslate"><span class="pre">urlize</span></code> 还将电子邮件地址转换为 <code class="docutils literal notranslate"><span class="pre">mailto:</span></code> 链接。如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"Send</span> <span class="pre">questions</span> <span class="pre">to</span> <span class="pre">foo@example.com"</span></code>，输出将是 <code class="docutils literal notranslate"><span class="pre">"Send</span> <span class="pre">questions</span> <span class="pre">to</span> <span class="pre">&lt;a</span> <span class="pre">href="mailto:foo@example.com"&gt;foo@example.com&lt;/a&gt;"</span></code>。</p>
<p><code class="docutils literal notranslate"><span class="pre">urlize</span></code> 过滤器还可以使用一个可选的参数 <code class="docutils literal notranslate"><span class="pre">autoescape</span></code>。如果 <code class="docutils literal notranslate"><span class="pre">autoescape</span></code> 为 <code class="docutils literal notranslate"><span class="pre">True</span></code>，则链接文本和 URL 将使用 Django 内置的 <a class="reference internal" href="#std:templatefilter-escape"><code class="xref std std-tfilter docutils literal notranslate"><span class="pre">escape</span></code></a> 过滤器进行转义。<code class="docutils literal notranslate"><span class="pre">autoescape</span></code> 的默认值是 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">如果 <code class="docutils literal notranslate"><span class="pre">urlize</span></code> 应用于已经包含 HTML 标记的文本，或者应用于包含单引号（<code class="docutils literal notranslate"><span class="pre">'</span></code>）的电子邮件地址，那么事情就不会像预期的那样进行。只对纯文本应用此过滤器。</p>
</div>
</div>
<div class="section" id="s-urlizetrunc">
<span id="s-std:templatefilter-urlizetrunc"></span><span id="urlizetrunc"></span><span id="std:templatefilter-urlizetrunc"></span><h3><code class="docutils literal notranslate"><span class="pre">urlizetrunc</span></code><a class="headerlink" href="#urlizetrunc" title="永久链接至标题">¶</a></h3>
<p>像 <a class="reference internal" href="#urlize">urlize</a> 一样，将 URL 和电子邮件地址转换为可点击的链接，但会截断长于给定字符限制的 URL。</p>
<p><strong>参数：</strong> 链接文本应截断的字符数，包括必要时添加的省略号。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">urlizetrunc</span><span class="o">:</span><span class="m">15</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"Check</span> <span class="pre">out</span> <span class="pre">www.djangoproject.com"</span></code>，则将输出 <code class="docutils literal notranslate"><span class="pre">'Check</span> <span class="pre">out</span> <span class="pre">&lt;a</span> <span class="pre">href="http://www.djangoproject.com"</span> <span class="pre">rel="nofollow"&gt;www.djangoproj…&lt;/a&gt;'</span></code>。</p>
<p>和 <a class="reference internal" href="#urlize">urlize</a> 一样，这个过滤器只能应用于纯文本。</p>
</div>
<div class="section" id="s-wordcount">
<span id="s-std:templatefilter-wordcount"></span><span id="wordcount"></span><span id="std:templatefilter-wordcount"></span><h3><code class="docutils literal notranslate"><span class="pre">wordcount</span></code><a class="headerlink" href="#wordcount" title="永久链接至标题">¶</a></h3>
<p>返回单词的数量。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">wordcount</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">"Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug"</span></code>，则输出将是 <code class="docutils literal notranslate"><span class="pre">4</span></code>。</p>
</div>
<div class="section" id="s-wordwrap">
<span id="s-std:templatefilter-wordwrap"></span><span id="wordwrap"></span><span id="std:templatefilter-wordwrap"></span><h3><code class="docutils literal notranslate"><span class="pre">wordwrap</span></code><a class="headerlink" href="#wordwrap" title="永久链接至标题">¶</a></h3>
<p>以指定的行长度包装文字。</p>
<p><strong>参数：</strong> 包裹文本的字符数</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">wordwrap</span><span class="o">:</span><span class="m">5</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>如果 <code class="docutils literal notranslate"><span class="pre">value</span></code> 是 <code class="docutils literal notranslate"><span class="pre">Joel</span> <span class="pre">is</span> <span class="pre">a</span> <span class="pre">slug</span></code>，则输出将是：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span>Joel
is a
slug
</pre></div>
</div>
</div>
<div class="section" id="s-yesno">
<span id="s-std:templatefilter-yesno"></span><span id="yesno"></span><span id="std:templatefilter-yesno"></span><h3><code class="docutils literal notranslate"><span class="pre">yesno</span></code><a class="headerlink" href="#yesno" title="永久链接至标题">¶</a></h3>
<p>将 <code class="docutils literal notranslate"><span class="pre">True</span></code>、<code class="docutils literal notranslate"><span class="pre">False</span></code> 和（可选的） <code class="docutils literal notranslate"><span class="pre">None</span></code> 值映射到字符串 “yes”、“no”、“maybe” 或以逗号分隔的列表形式传递的自定义映射，并根据值返回其中一个字符串。</p>
<p>例子：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">yesno</span><span class="s2">:"yeah,no,maybe"</span> <span class="cp">}}</span>
</pre></div>
</div>
<table class="docutils">
<colgroup>
<col width="13%">
<col width="29%">
<col width="57%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">值</th>
<th class="head">参数</th>
<th class="head">输出</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">True</span></code></td>
<td>&nbsp;</td>
<td><code class="docutils literal notranslate"><span class="pre">yes</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">True</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">"yeah,no,maybe"</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">yeah</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">False</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">"yeah,no,maybe"</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">no</span></code></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">None</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">"yeah,no,maybe"</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">maybe</span></code></td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">None</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">"yeah,no"</span></code></td>
<td><code class="docutils literal notranslate"><span class="pre">no</span></code> （如果没有给出 <code class="docutils literal notranslate"><span class="pre">None</span></code> 的映射，则将 <code class="docutils literal notranslate"><span class="pre">None</span></code> 转换为 <code class="docutils literal notranslate"><span class="pre">False</span></code>）</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="s-internationalization-tags-and-filters">
<span id="internationalization-tags-and-filters"></span><h2>国际化标签和过滤器<a class="headerlink" href="#internationalization-tags-and-filters" title="永久链接至标题">¶</a></h2>
<p>Django 提供了模板标签和过滤器来控制模板中的 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/topics/i18n/"><span class="doc">国际化</span></a> 的各个环节。它们允许对翻译、格式化和时区转换进行细化控制。</p>
<div class="section" id="s-i18n">
<span id="i18n"></span><h3><code class="docutils literal notranslate"><span class="pre">i18n</span></code><a class="headerlink" href="#i18n" title="永久链接至标题">¶</a></h3>
<p>该库允许在模板中指定可翻译的文本。要启用它，请将 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-USE_I18N"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_I18N</span></code></a> 设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>，然后用 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">i18n</span> <span class="pre">%}</span></code> 加载它。</p>
<p>参见 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/topics/i18n/translation/#specifying-translation-strings-in-template-code"><span class="std std-ref">在模板代码中国际化</span></a>。</p>
</div>
<div class="section" id="s-l10n">
<span id="l10n"></span><h3><code class="docutils literal notranslate"><span class="pre">l10n</span></code><a class="headerlink" href="#l10n" title="永久链接至标题">¶</a></h3>
<p>这个库提供了对模板中值的本地化控制。你只需要使用 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">l10n</span> <span class="pre">%}</span></code> 加载该库，但你通常会将 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-USE_L10N"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_L10N</span></code></a> 设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>，这样默认情况下本地化是有效的。</p>
<p>参见 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/topics/i18n/formatting/#topic-l10n-templates"><span class="std std-ref">在模板中控制本地化</span></a>。</p>
</div>
<div class="section" id="s-tz">
<span id="tz"></span><h3><code class="docutils literal notranslate"><span class="pre">tz</span></code><a class="headerlink" href="#tz" title="永久链接至标题">¶</a></h3>
<p>这个库提供了对模板中时区转换的控制。和 <code class="docutils literal notranslate"><span class="pre">l10n</span></code> 一样，你只需要使用 <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">tz</span> <span class="pre">%}</span></code> 加载该库，但你通常也会将 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-USE_TZ"><code class="xref std std-setting docutils literal notranslate"><span class="pre">USE_TZ</span></code></a> 设置为 <code class="docutils literal notranslate"><span class="pre">True</span></code>，这样就会默认转换为当地时间。</p>
<p>参见 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/topics/i18n/timezones/#time-zones-in-templates"><span class="std std-ref">模板中时区感知(aware)输出</span></a>。</p>
</div>
</div>
<div class="section" id="s-other-tags-and-filters-libraries">
<span id="other-tags-and-filters-libraries"></span><h2>其他标签和过滤器库<a class="headerlink" href="#other-tags-and-filters-libraries" title="永久链接至标题">¶</a></h2>
<p>Django 自带了一些其他的模板标签库，你必须在你的 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-INSTALLED_APPS"><code class="xref std std-setting docutils literal notranslate"><span class="pre">INSTALLED_APPS</span></code></a> 配置中明确启用，并在你的模板中用 <a class="reference internal" href="#std:templatetag-load"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">{%</span> <span class="pre">load</span> <span class="pre">%}</span></code></a> 标签启用。</p>
<div class="section" id="s-django-contrib-humanize">
<span id="django-contrib-humanize"></span><h3><code class="docutils literal notranslate"><span class="pre">django.contrib.humanize</span></code><a class="headerlink" href="#django-contrib-humanize" title="永久链接至标题">¶</a></h3>
<p>一组 Django 模板过滤器，用于给数据添加 “人情味”。参见 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/contrib/humanize/"><span class="doc">django.contrib.humanize</span></a>。</p>
</div>
<div class="section" id="s-static">
<span id="static"></span><h3><code class="docutils literal notranslate"><span class="pre">static</span></code><a class="headerlink" href="#static" title="永久链接至标题">¶</a></h3>
<div class="section" id="s-std:templatetag-static">
<span id="s-id7"></span><span id="std:templatetag-static"></span><span id="id7"></span><h4><code class="docutils literal notranslate"><span class="pre">static</span></code><a class="headerlink" href="#std:templatetag-static" title="永久链接至标题">¶</a></h4>
<p>要链接到保存在 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-STATIC_ROOT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_ROOT</span></code></a> 中的静态文件，Django 有一个 <a class="reference internal" href="#std:templatetag-static"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">static</span></code></a> 模板标签。如果安装了 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/contrib/staticfiles/#module-django.contrib.staticfiles" title="django.contrib.staticfiles: An app for handling static files."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.staticfiles</span></code></a> 应用程序，该标签将使用 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-STATICFILES_STORAGE"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATICFILES_STORAGE</span></code></a> 指定的存储空间的 <code class="docutils literal notranslate"><span class="pre">url()</span></code> 方法来提供文件。例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">static</span> <span class="cp">%}</span>
<span class="p">&lt;</span><span class="nt">img</span> <span class="na">src</span><span class="o">=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">static</span> <span class="s1">'images/hi.jpg'</span> <span class="cp">%}</span><span class="s">"</span> <span class="na">alt</span><span class="o">=</span><span class="s">"Hi!"</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>它还能够消耗标准的上下文变量，例如，假设一个 <code class="docutils literal notranslate"><span class="pre">user_stylesheet</span></code> 变量被传递给模板：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">static</span> <span class="cp">%}</span>
<span class="p">&lt;</span><span class="nt">link</span> <span class="na">rel</span><span class="o">=</span><span class="s">"stylesheet"</span> <span class="na">href</span><span class="o">=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">static</span> <span class="nv">user_stylesheet</span> <span class="cp">%}</span><span class="s">"</span> <span class="na">type</span><span class="o">=</span><span class="s">"text/css"</span> <span class="na">media</span><span class="o">=</span><span class="s">"screen"</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>如果你想检索一个静态的 URL 而不显示它，你可以使用一个稍微不同的调用：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">static</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">static</span> <span class="s2">"images/hi.jpg"</span> <span class="k">as</span> <span class="nv">myphoto</span> <span class="cp">%}</span>
<span class="p">&lt;</span><span class="nt">img</span> <span class="na">src</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">myphoto</span> <span class="cp">}}</span><span class="s">"</span><span class="p">&gt;</span>
</pre></div>
</div>
<div class="admonition-using-jinja2-templates admonition">
<p class="first admonition-title">使用 Jinja2 模板？</p>
<p class="last">参见 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/topics/templates/#django.template.backends.jinja2.Jinja2" title="django.template.backends.jinja2.Jinja2"><code class="xref py py-class docutils literal notranslate"><span class="pre">Jinja2</span></code></a>，了解如何在 Jinja2 中使用 <code class="docutils literal notranslate"><span class="pre">static</span></code> 标签。</p>
</div>
</div>
<div class="section" id="s-get-static-prefix">
<span id="s-std:templatetag-get_static_prefix"></span><span id="get-static-prefix"></span><span id="std:templatetag-get_static_prefix"></span><h4><code class="docutils literal notranslate"><span class="pre">get_static_prefix</span></code><a class="headerlink" href="#get-static-prefix" title="永久链接至标题">¶</a></h4>
<p>你应该更喜欢使用 <a class="reference internal" href="#std:templatetag-static"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">static</span></code></a> 模板标签，但如果你需要更多的控制 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-STATIC_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">STATIC_URL</span></code></a> 被注入模板的具体位置和方式，你可以使用 <a class="reference internal" href="#std:templatetag-get_static_prefix"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">get_static_prefix</span></code></a> 模板标签：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">static</span> <span class="cp">%}</span>
<span class="p">&lt;</span><span class="nt">img</span> <span class="na">src</span><span class="o">=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">get_static_prefix</span> <span class="cp">%}</span><span class="s">images/hi.jpg"</span> <span class="na">alt</span><span class="o">=</span><span class="s">"Hi!"</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>如果你多次需要数值，还可以使用第二种形式来避免额外的处理：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">static</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">get_static_prefix</span> <span class="k">as</span> <span class="nv">STATIC_PREFIX</span> <span class="cp">%}</span>

<span class="p">&lt;</span><span class="nt">img</span> <span class="na">src</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">STATIC_PREFIX</span> <span class="cp">}}</span><span class="s">images/hi.jpg"</span> <span class="na">alt</span><span class="o">=</span><span class="s">"Hi!"</span><span class="p">&gt;</span>
<span class="p">&lt;</span><span class="nt">img</span> <span class="na">src</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">STATIC_PREFIX</span> <span class="cp">}}</span><span class="s">images/hi2.jpg"</span> <span class="na">alt</span><span class="o">=</span><span class="s">"Hello!"</span><span class="p">&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="s-get-media-prefix">
<span id="s-std:templatetag-get_media_prefix"></span><span id="get-media-prefix"></span><span id="std:templatetag-get_media_prefix"></span><h4><code class="docutils literal notranslate"><span class="pre">get_media_prefix</span></code><a class="headerlink" href="#get-media-prefix" title="永久链接至标题">¶</a></h4>
<p>与 <a class="reference internal" href="#std:templatetag-get_static_prefix"><code class="xref std std-ttag docutils literal notranslate"><span class="pre">get_static_prefix</span></code></a> 类似，<code class="docutils literal notranslate"><span class="pre">get_media_prefix</span></code> 用媒体前缀 <a class="reference internal" href="https://docs.djangoproject.com/zh-hans/3.2/ref/settings/#std:setting-MEDIA_URL"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_URL</span></code></a> 填充一个模板变量，例如：</p>
<div class="highlight-html+django notranslate"><div class="highlight"><pre class="notranslate"><span></span><span class="cp">{%</span> <span class="k">load</span> <span class="nv">static</span> <span class="cp">%}</span>
<span class="p">&lt;</span><span class="nt">body</span> <span class="na">data-media-url</span><span class="o">=</span><span class="s">"</span><span class="cp">{%</span> <span class="k">get_media_prefix</span> <span class="cp">%}</span><span class="s">"</span><span class="p">&gt;</span>
</pre></div>
</div>
<p>通过将值存储在数据属性中，我们可以确保在 JavaScript 上下文中使用该值时适当地进行转义。</p>
</div>
</div>
</div>
</div>

</div>



<div class="browse-horizontal">
  
  <div class="left"><a href="https://docs.djangoproject.com/zh-hans/3.2/ref/templates/language/"><i class="icon icon-chevron-left"></i> Django 模板语言</a></div>
  
  
  <div class="right"><a href="https://docs.djangoproject.com/zh-hans/3.2/ref/templates/api/">Django 模板语言：对于 Python 开发者 <i class="icon icon-chevron-right"></i></a></div>
  
</div>



        <a href="#top" class="backtotop"><i class="icon icon-chevron-up"></i> Back to Top</a>
      </div>

      
<h1 class="visuallyhidden">附加信息</h1>
<div role="complementary">

  


  <div class="fundraising-sidebar">
    <h2>Support Django!</h2>

    <div class="small-heart">
      <img src="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/fundraising-heart.svg" alt="Support Django!">
    </div>

    <div class="small-cta">
      <ul class="list-links-small">
        <li><a href="https://www.djangoproject.com/fundraising/">
          Fakhrul Adli donated to the Django Software Foundation to
          support Django development. Donate today!
        </a></li>
      </ul>
    </div>
  </div>



  
    <h2>内容</h2>
    
    <ul>
<li><a class="reference internal" href="#">内置模板标签和过滤器</a><ul>
<li><a class="reference internal" href="#built-in-tag-reference">内置标签参考</a><ul>
<li><a class="reference internal" href="#autoescape"><code class="docutils literal notranslate"><span class="pre">autoescape</span></code></a></li>
<li><a class="reference internal" href="#block"><code class="docutils literal notranslate"><span class="pre">block</span></code></a></li>
<li><a class="reference internal" href="#comment"><code class="docutils literal notranslate"><span class="pre">comment</span></code></a></li>
<li><a class="reference internal" href="#csrf-token"><code class="docutils literal notranslate"><span class="pre">csrf_token</span></code></a></li>
<li><a class="reference internal" href="#cycle"><code class="docutils literal notranslate"><span class="pre">cycle</span></code></a></li>
<li><a class="reference internal" href="#debug"><code class="docutils literal notranslate"><span class="pre">debug</span></code></a></li>
<li><a class="reference internal" href="#extends"><code class="docutils literal notranslate"><span class="pre">extends</span></code></a></li>
<li><a class="reference internal" href="#filter"><code class="docutils literal notranslate"><span class="pre">filter</span></code></a></li>
<li><a class="reference internal" href="#firstof"><code class="docutils literal notranslate"><span class="pre">firstof</span></code></a></li>
<li><a class="reference internal" href="#for"><code class="docutils literal notranslate"><span class="pre">for</span></code></a></li>
<li><a class="reference internal" href="#for-empty"><code class="docutils literal notranslate"><span class="pre">for</span></code> ... <code class="docutils literal notranslate"><span class="pre">empty</span></code></a></li>
<li><a class="reference internal" href="#if"><code class="docutils literal notranslate"><span class="pre">if</span></code></a><ul>
<li><a class="reference internal" href="#boolean-operators">布尔操作</a><ul>
<li><a class="reference internal" href="#operator"><code class="docutils literal notranslate"><span class="pre">==</span></code> 运算符</a></li>
<li><a class="reference internal" href="#id1"><code class="docutils literal notranslate"><span class="pre">!=</span></code> 运算符</a></li>
<li><a class="reference internal" href="#id2"><code class="docutils literal notranslate"><span class="pre">&lt;</span></code> 运算符</a></li>
<li><a class="reference internal" href="#id3"><code class="docutils literal notranslate"><span class="pre">&gt;</span></code> 运算符</a></li>
<li><a class="reference internal" href="#id4"><code class="docutils literal notranslate"><span class="pre">&lt;=</span></code> 运算符</a></li>
<li><a class="reference internal" href="#id5"><code class="docutils literal notranslate"><span class="pre">&gt;=</span></code> 运算符</a></li>
<li><a class="reference internal" href="#in-operator"><code class="docutils literal notranslate"><span class="pre">in</span></code> 运算符</a></li>
<li><a class="reference internal" href="#not-in-operator"><code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">in</span></code> 操作符</a></li>
<li><a class="reference internal" href="#is-operator"><code class="docutils literal notranslate"><span class="pre">is</span></code> 运算符</a></li>
<li><a class="reference internal" href="#is-not-operator"><code class="docutils literal notranslate"><span class="pre">is</span> <span class="pre">not</span></code> 运算符</a></li>
</ul>
</li>
<li><a class="reference internal" href="#filters">过滤器</a></li>
<li><a class="reference internal" href="#complex-expressions">复合表达式</a></li>
</ul>
</li>
<li><a class="reference internal" href="#ifequal-and-ifnotequal"><code class="docutils literal notranslate"><span class="pre">ifequal</span></code> 和 <code class="docutils literal notranslate"><span class="pre">ifnotequal</span></code></a></li>
<li><a class="reference internal" href="#ifchanged"><code class="docutils literal notranslate"><span class="pre">ifchanged</span></code></a></li>
<li><a class="reference internal" href="#include"><code class="docutils literal notranslate"><span class="pre">include</span></code></a></li>
<li><a class="reference internal" href="#load"><code class="docutils literal notranslate"><span class="pre">load</span></code></a></li>
<li><a class="reference internal" href="#lorem"><code class="docutils literal notranslate"><span class="pre">lorem</span></code></a></li>
<li><a class="reference internal" href="#now"><code class="docutils literal notranslate"><span class="pre">now</span></code></a></li>
<li><a class="reference internal" href="#regroup"><code class="docutils literal notranslate"><span class="pre">regroup</span></code></a><ul>
<li><a class="reference internal" href="#grouping-on-other-properties">对其他属性进行分组</a></li>
</ul>
</li>
<li><a class="reference internal" href="#resetcycle"><code class="docutils literal notranslate"><span class="pre">resetcycle</span></code></a></li>
<li><a class="reference internal" href="#spaceless"><code class="docutils literal notranslate"><span class="pre">spaceless</span></code></a></li>
<li><a class="reference internal" href="#templatetag"><code class="docutils literal notranslate"><span class="pre">templatetag</span></code></a></li>
<li><a class="reference internal" href="#url"><code class="docutils literal notranslate"><span class="pre">url</span></code></a></li>
<li><a class="reference internal" href="#verbatim"><code class="docutils literal notranslate"><span class="pre">verbatim</span></code></a></li>
<li><a class="reference internal" href="#widthratio"><code class="docutils literal notranslate"><span class="pre">widthratio</span></code></a></li>
<li><a class="reference internal" href="#with"><code class="docutils literal notranslate"><span class="pre">with</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#built-in-filter-reference">内置过滤器参考</a><ul>
<li><a class="reference internal" href="#add"><code class="docutils literal notranslate"><span class="pre">add</span></code></a></li>
<li><a class="reference internal" href="#addslashes"><code class="docutils literal notranslate"><span class="pre">addslashes</span></code></a></li>
<li><a class="reference internal" href="#capfirst"><code class="docutils literal notranslate"><span class="pre">capfirst</span></code></a></li>
<li><a class="reference internal" href="#center"><code class="docutils literal notranslate"><span class="pre">center</span></code></a></li>
<li><a class="reference internal" href="#cut"><code class="docutils literal notranslate"><span class="pre">cut</span></code></a></li>
<li><a class="reference internal" href="#date"><code class="docutils literal notranslate"><span class="pre">date</span></code></a></li>
<li><a class="reference internal" href="#default"><code class="docutils literal notranslate"><span class="pre">default</span></code></a></li>
<li><a class="reference internal" href="#default-if-none"><code class="docutils literal notranslate"><span class="pre">default_if_none</span></code></a></li>
<li><a class="reference internal" href="#dictsort"><code class="docutils literal notranslate"><span class="pre">dictsort</span></code></a></li>
<li><a class="reference internal" href="#dictsortreversed"><code class="docutils literal notranslate"><span class="pre">dictsortreversed</span></code></a></li>
<li><a class="reference internal" href="#divisibleby"><code class="docutils literal notranslate"><span class="pre">divisibleby</span></code></a></li>
<li><a class="reference internal" href="#escape"><code class="docutils literal notranslate"><span class="pre">escape</span></code></a></li>
<li><a class="reference internal" href="#escapejs"><code class="docutils literal notranslate"><span class="pre">escapejs</span></code></a></li>
<li><a class="reference internal" href="#filesizeformat"><code class="docutils literal notranslate"><span class="pre">filesizeformat</span></code></a></li>
<li><a class="reference internal" href="#first"><code class="docutils literal notranslate"><span class="pre">first</span></code></a></li>
<li><a class="reference internal" href="#floatformat"><code class="docutils literal notranslate"><span class="pre">floatformat</span></code></a></li>
<li><a class="reference internal" href="#force-escape"><code class="docutils literal notranslate"><span class="pre">force_escape</span></code></a></li>
<li><a class="reference internal" href="#get-digit"><code class="docutils literal notranslate"><span class="pre">get_digit</span></code></a></li>
<li><a class="reference internal" href="#iriencode"><code class="docutils literal notranslate"><span class="pre">iriencode</span></code></a></li>
<li><a class="reference internal" href="#join"><code class="docutils literal notranslate"><span class="pre">join</span></code></a></li>
<li><a class="reference internal" href="#json-script"><code class="docutils literal notranslate"><span class="pre">json_script</span></code></a></li>
<li><a class="reference internal" href="#last"><code class="docutils literal notranslate"><span class="pre">last</span></code></a></li>
<li><a class="reference internal" href="#length"><code class="docutils literal notranslate"><span class="pre">length</span></code></a></li>
<li><a class="reference internal" href="#length-is"><code class="docutils literal notranslate"><span class="pre">length_is</span></code></a></li>
<li><a class="reference internal" href="#linebreaks"><code class="docutils literal notranslate"><span class="pre">linebreaks</span></code></a></li>
<li><a class="reference internal" href="#linebreaksbr"><code class="docutils literal notranslate"><span class="pre">linebreaksbr</span></code></a></li>
<li><a class="reference internal" href="#linenumbers"><code class="docutils literal notranslate"><span class="pre">linenumbers</span></code></a></li>
<li><a class="reference internal" href="#ljust"><code class="docutils literal notranslate"><span class="pre">ljust</span></code></a></li>
<li><a class="reference internal" href="#lower"><code class="docutils literal notranslate"><span class="pre">lower</span></code></a></li>
<li><a class="reference internal" href="#make-list"><code class="docutils literal notranslate"><span class="pre">make_list</span></code></a></li>
<li><a class="reference internal" href="#phone2numeric"><code class="docutils literal notranslate"><span class="pre">phone2numeric</span></code></a></li>
<li><a class="reference internal" href="#pluralize"><code class="docutils literal notranslate"><span class="pre">pluralize</span></code></a></li>
<li><a class="reference internal" href="#pprint"><code class="docutils literal notranslate"><span class="pre">pprint</span></code></a></li>
<li><a class="reference internal" href="#random"><code class="docutils literal notranslate"><span class="pre">random</span></code></a></li>
<li><a class="reference internal" href="#rjust"><code class="docutils literal notranslate"><span class="pre">rjust</span></code></a></li>
<li><a class="reference internal" href="#safe"><code class="docutils literal notranslate"><span class="pre">safe</span></code></a></li>
<li><a class="reference internal" href="#safeseq"><code class="docutils literal notranslate"><span class="pre">safeseq</span></code></a></li>
<li><a class="reference internal" href="#slice"><code class="docutils literal notranslate"><span class="pre">slice</span></code></a></li>
<li><a class="reference internal" href="#slugify"><code class="docutils literal notranslate"><span class="pre">slugify</span></code></a></li>
<li><a class="reference internal" href="#stringformat"><code class="docutils literal notranslate"><span class="pre">stringformat</span></code></a></li>
<li><a class="reference internal" href="#striptags"><code class="docutils literal notranslate"><span class="pre">striptags</span></code></a></li>
<li><a class="reference internal" href="#time"><code class="docutils literal notranslate"><span class="pre">time</span></code></a></li>
<li><a class="reference internal" href="#timesince"><code class="docutils literal notranslate"><span class="pre">timesince</span></code></a></li>
<li><a class="reference internal" href="#timeuntil"><code class="docutils literal notranslate"><span class="pre">timeuntil</span></code></a></li>
<li><a class="reference internal" href="#title"><code class="docutils literal notranslate"><span class="pre">title</span></code></a></li>
<li><a class="reference internal" href="#truncatechars"><code class="docutils literal notranslate"><span class="pre">truncatechars</span></code></a></li>
<li><a class="reference internal" href="#truncatechars-html"><code class="docutils literal notranslate"><span class="pre">truncatechars_html</span></code></a></li>
<li><a class="reference internal" href="#truncatewords"><code class="docutils literal notranslate"><span class="pre">truncatewords</span></code></a></li>
<li><a class="reference internal" href="#truncatewords-html"><code class="docutils literal notranslate"><span class="pre">truncatewords_html</span></code></a></li>
<li><a class="reference internal" href="#unordered-list"><code class="docutils literal notranslate"><span class="pre">unordered_list</span></code></a></li>
<li><a class="reference internal" href="#upper"><code class="docutils literal notranslate"><span class="pre">upper</span></code></a></li>
<li><a class="reference internal" href="#urlencode"><code class="docutils literal notranslate"><span class="pre">urlencode</span></code></a></li>
<li><a class="reference internal" href="#urlize"><code class="docutils literal notranslate"><span class="pre">urlize</span></code></a></li>
<li><a class="reference internal" href="#urlizetrunc"><code class="docutils literal notranslate"><span class="pre">urlizetrunc</span></code></a></li>
<li><a class="reference internal" href="#wordcount"><code class="docutils literal notranslate"><span class="pre">wordcount</span></code></a></li>
<li><a class="reference internal" href="#wordwrap"><code class="docutils literal notranslate"><span class="pre">wordwrap</span></code></a></li>
<li><a class="reference internal" href="#yesno"><code class="docutils literal notranslate"><span class="pre">yesno</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#internationalization-tags-and-filters">国际化标签和过滤器</a><ul>
<li><a class="reference internal" href="#i18n"><code class="docutils literal notranslate"><span class="pre">i18n</span></code></a></li>
<li><a class="reference internal" href="#l10n"><code class="docutils literal notranslate"><span class="pre">l10n</span></code></a></li>
<li><a class="reference internal" href="#tz"><code class="docutils literal notranslate"><span class="pre">tz</span></code></a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-tags-and-filters-libraries">其他标签和过滤器库</a><ul>
<li><a class="reference internal" href="#django-contrib-humanize"><code class="docutils literal notranslate"><span class="pre">django.contrib.humanize</span></code></a></li>
<li><a class="reference internal" href="#static"><code class="docutils literal notranslate"><span class="pre">static</span></code></a><ul>
<li><a class="reference internal" href="#std:templatetag-static"><code class="docutils literal notranslate"><span class="pre">static</span></code></a></li>
<li><a class="reference internal" href="#get-static-prefix"><code class="docutils literal notranslate"><span class="pre">get_static_prefix</span></code></a></li>
<li><a class="reference internal" href="#get-media-prefix"><code class="docutils literal notranslate"><span class="pre">get_media_prefix</span></code></a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>

    
  

  
  <h2>浏览</h2>
  <ul>
    
    
    <li>上一个： <a href="https://docs.djangoproject.com/zh-hans/3.2/ref/templates/language/">Django 模板语言</a></li>
    
    
    <li>下一个： <a href="https://docs.djangoproject.com/zh-hans/3.2/ref/templates/api/">Django 模板语言：对于 Python 开发者</a></li>
    
    <li><a href="https://docs.djangoproject.com/zh-hans/3.2/contents/">目录</a></li>
    
    <li><a href="https://docs.djangoproject.com/zh-hans/3.2/genindex/">总目录</a></li>
    
    <li><a href="https://docs.djangoproject.com/zh-hans/3.2/py-modindex/">Python 模块索引</a></li>
    
    
  </ul>
  

  
  <h2>当前位置</h2>
  <ul>
    <li>
      <a href="https://docs.djangoproject.com/zh-hans/3.2/">Django 3.2 文档</a>
      
      <ul><li><a href="https://docs.djangoproject.com/zh-hans/3.2/ref/">API 参考</a>
      
      <ul><li><a href="https://docs.djangoproject.com/zh-hans/3.2/ref/templates/">模板</a>
      
      <ul><li>内置模板标签和过滤器</li></ul>
      </li></ul></li></ul>
    </li>
  </ul>
  

  
  <h2 id="getting-help-sidebar">获取帮助</h2>
  <dl class="list-links">
    <dt><a href="https://docs.djangoproject.com/zh-hans/3.2/faq/">FAQ</a></dt>
    <dd>尝试查看 FAQ — 它包括了很多常见问题的答案</dd>

    <dt><a href="https://docs.djangoproject.com/en/stable/genindex/">索引</a>, <a href="https://docs.djangoproject.com/en/stable/py-modindex/">模块索引</a>, or <a href="https://docs.djangoproject.com/en/stable/contents/">目录</a></dt>
    <dd>查找特定信息时比较容易</dd>

    <dt><a href="https://groups.google.com/group/django-users/">django-users mailing list</a></dt>
    <dd>在 django-users 邮件列表存档中搜索信息，或者发布一个问题。</dd>

    <dt><a href="irc://irc.libera.chat/django">#django IRC channel</a></dt>
    <dd>在 #django IRC 频道上提问，或者搜索 IRC 历史找到相似的问题与解答。</dd>

    <dt><a href="https://code.djangoproject.com/">Ticket tracker</a></dt>
    <dd>在我们的 `ticket tracker`_ 中报告 Django 或 Django 文档的 Bug。</dd>
  </dl>
  

  
  <h2>下载：</h2>
  <p>
    离线 (Django 3.2):
    <a href="https://media.djangoproject.com/docs/django-docs-3.2-zh-hans.zip">HTML</a> |
    <a href="https://media.readthedocs.org/pdf/django/3.2.x/django.pdf">PDF</a> |
    <a href="https://media.readthedocs.org/epub/django/3.2.x/django.epub">ePub</a>
    <br>
    <span class="quiet">
      由 <a href="https://readthedocs.org/">Read the Docs</a> 提供。
    </span>
  </p>
  
</div>

      

    </div>

     
     

    
    
    

    
      
<div role="contentinfo">
  <div class="subfooter">
    <div class="container">
      <h1 class="visuallyhidden">Django Links</h1>

      <div class="col learn">
        <h2>Learn More</h2>
        <ul>
          <li><a href="https://www.djangoproject.com/start/overview/">About Django</a></li>
          
          <li><a href="https://www.djangoproject.com/start/">Getting Started with Django</a></li>
          <li><a href="https://docs.djangoproject.com/en/dev/internals/organization/">Team Organization</a></li>
          <li><a href="https://www.djangoproject.com/foundation/">Django Software Foundation</a></li>
          <li><a href="https://www.djangoproject.com/conduct/">Code of Conduct</a></li>
          <li><a href="https://www.djangoproject.com/diversity/">Diversity Statement</a></li>
        </ul>
      </div>

      <div class="col involved">
        <h2>Get Involved</h2>
        <ul>
          <li><a href="https://www.djangoproject.com/community/">Join a Group</a></li>
          <li><a href="https://docs.djangoproject.com/en/dev/internals/contributing/">Contribute to Django</a></li>
          <li><a href="https://docs.djangoproject.com/en/dev/internals/contributing/bugs-and-features/">Submit a Bug</a></li>
          <li><a href="https://docs.djangoproject.com/en/dev/internals/security/#reporting-security-issues">Report a Security Issue</a></li>
        </ul>
      </div>

      <div class="col follow last-child">
        <h2>Follow Us</h2>
        <ul>
          <li><a href="https://github.com/django">GitHub</a></li>
          <li><a href="https://twitter.com/djangoproject">Twitter</a></li>
          <li><a href="https://www.djangoproject.com/rss/weblog/">News RSS</a></li>
          <li><a href="https://groups.google.com/forum/#!forum/django-users">Django Users Mailing List</a></li>
        </ul>
      </div>

      <div class="col follow last-child">
        <h2>Support Us</h2>
        <ul>
          <li><a href="https://www.djangoproject.com/fundraising/">Sponsor Django</a></li>
          <li><a href="https://django.threadless.com/" target="_blank">Official merchandise store</a></li>
          <li><a href="https://docs.djangoproject.com/foundation/donate/#amazon-smile">Amazon Smile</a></li>
          <li><a href="https://docs.djangoproject.com/foundation/donate/#benevity-giving">Benevity Workplace Giving Program</a></li>
        </ul>
      </div>

    </div>
  </div>

  <div class="footer">
    <div class="container">
      <div class="footer-logo">
        <a class="logo" href="https://www.djangoproject.com/">Django</a>
      </div>
      <ul class="thanks">
        <li>
          <span>Hosting by</span> <a class="rackspace" href="https://www.rackspace.com/">Rackspace</a>
        </li>
        <li class="design"><span>Design by</span> <a class="threespot" href="https://www.threespot.com/">Threespot</a> <span class="ampersand">&amp;</span> <a class="andrevv" href="http://andrevv.com/">andrevv</a></li>
      </ul>
      <p class="copyright">© 2005-2022
        <a href="https://www.djangoproject.com/foundation/"> Django Software
        Foundation</a> and individual contributors. Django is a
        <a href="https://www.djangoproject.com/trademarks/">registered
        trademark</a> of the Django Software Foundation.
      </p>
    </div>
  </div>

</div>

    

    
    <script>
    function extless(input) {
        return input.replace(/(.*)\.[^.]+$/, '$1');
    }
    var require = {
        shim: {
            'jquery': [],
            'jquery.inview': ["jquery"],
            'jquery.payment': ["jquery"],
            'jquery.flot': ["jquery"],
            'jquery.unveil': ["jquery"],
            'stripe': {
              exports: 'Stripe'
            }
        },
        paths: {
            "jquery": extless("https://static.djangoproject.com/js/lib/jquery/dist/jquery.min.87e69028f78d.js"),
            "jquery.inview": extless("https://static.djangoproject.com/js/lib/jquery.inview/jquery.inview.min.4edba1c65592.js"),
            "jquery.payment": extless("https://static.djangoproject.com/js/lib/jquery.payment/lib/jquery.payment.e99c05ca79ae.js"),
            "jquery.unveil": extless("https://static.djangoproject.com/js/lib/unveil/jquery.unveil.min.ac79eb277093.js"),
            "jquery.flot": extless("https://static.djangoproject.com/js/lib/jquery-flot/jquery.flot.min.9964206e9d7f.js"),
            "clipboard": extless("https://static.djangoproject.com/js/lib/clipboard/dist/clipboard.min.bd70fd596a23.js"),
            "mod/floating-warning": extless("https://static.djangoproject.com/js/mod/floating-warning.a21b2abd2884.js"),
            "mod/list-collapsing": extless("https://static.djangoproject.com/js/mod/list-collapsing.c1a08d3ef9e9.js"),
            "mod/list-feature": extless("https://static.djangoproject.com/js/mod/list-feature.73529480f25b.js"),
            "mod/mobile-menu": extless("https://static.djangoproject.com/js/mod/mobile-menu.841726ee903a.js"),
            "mod/version-switcher": extless("https://static.djangoproject.com/js/mod/version-switcher.c28bb83972bb.js"),
            "mod/search-key": extless("https://static.djangoproject.com/js/mod/search-key.f3c0a6fcfedd.js"),
            "mod/stripe-change-card": extless("https://static.djangoproject.com/js/mod/stripe-change-card.682c710317a8.js"),
            "mod/console-tabs": extless("https://static.djangoproject.com/js/mod/console-tabs.70ce882faaf3.js"),
            "stripe-checkout": "https://checkout.stripe.com/checkout",
            "stripe": "https://js.stripe.com/v3/?" // ? needed due to require.js
        }
    };
    </script>
    <script data-main="https://static.djangoproject.com/js/main.2f2c378dba28.js" src="%E5%86%85%E7%BD%AE%E6%A8%A1%E6%9D%BF%E6%A0%87%E7%AD%BE%E5%92%8C%E8%BF%87%E6%BB%A4%E5%99%A8%20Django%20%E6%96%87%E6%A1%A3%20Django_files/require.js"></script>
    



  

<remove-web-limits-iqxin id="rwl-iqxin" class="rwl-exempt" style="position: fixed; top: 11px; left: 0px;"><qxinbutton type="qxinbutton" id="rwl-setbtn"> set </qxinbutton> <lalala style="cursor:move; font-size:12px;">限制解除</lalala> <input type="checkbox" name="" id="black_node"><style type="text/css">#rwl-iqxin{position:fixed;transform:translate(-95px,0);width:85px;height:25px;font-size:12px;font-weight: 500;font-family:Verdana, Arial, '宋体';color:#fff;background:#333;z-index:2147483647;margin: 0;opacity:0.05;transition:0.3s;overflow:hidden;user-select:none;text-align:center;white-space:nowrap;line-height:25px;padding:0 16px;border:1px solid #ccc;border-width:1px 1px 1px 0;border-bottom-right-radius:5px;box-sizing: content-box;}#rwl-iqxin input{margin: 0;padding: 0;vertical-align:middle;-webkit-appearance:checkbox;-moz-appearance:checkbox;position: static;clip: auto;opacity: 1;cursor: pointer;}#rwl-iqxin.rwl-active-iqxin{left: 0px;transform:translate(0,0);opacity: 0.9;height: 32px;line-height: 32px}#rwl-iqxin label{margin:0;padding:0;font-weight:500;}#rwl-iqxin #rwl-setbtn{margin: 0 4px 0 0;padding: 0 0 0 4px;border: none;border-radius: 2px;cursor: pointer;background: #fff;color: #000;} </style></remove-web-limits-iqxin><div style="position: fixed; right: 25px; bottom: 25px; cursor: pointer; z-index: 2147483647; user-select: none;" class="no-print" id="darkBtn"><div style="background: #333;display: flex;align-items: center;justify-content: center;width: 30px;height: 30px;border-radius: 50%"><svg style="position: static;margin: 0;padding: 0;" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="20" height="20"><path d="M513.173 128A255.061 255.061 0 0 0 448 298.667c0 141.376 114.624 256 256 256a255.36 255.36 0 0 0 189.803-84.203A392.855 392.855 0 0 1 896 512c0 212.075-171.925 384-384 384S128 724.075 128 512c0-209.707 168.107-380.16 376.96-383.936l8.192-.064zM395.35 213.93l-3.52 1.409C274.645 262.827 192 377.77 192 512c0 176.725 143.275 320 320 320 145.408 0 268.16-96.981 307.115-229.803l1.536-5.504-1.6.64a319.51 319.51 0 0 1-106.496 21.227l-8.555.107c-176.725 0-320-143.275-320-320 0-28.48 3.755-56.406 10.944-83.2l.405-1.536z" fill="#adbac7"></path></svg></div></div><div id="goog-gt-tt" class="skiptranslate" dir="ltr"><div style="padding: 8px;"><div><div class="logo"><span id="lb" src="" alt="Google 翻译" width="20" height="20"></span></div></div></div><div class="top" style="padding: 8px; float: left; width: 100%;"><h1 class="title gray">原文</h1></div><div class="middle" style="padding: 8px;"><div class="original-text"></div></div><div class="bottom" style="padding: 8px;"><div class="activity-links"><span class="activity-link">提供更好的翻译建议</span><span class="activity-link"></span></div><div class="started-activity-container"><hr style="color: #CCC; background-color: #CCC; height: 1px; border: none;"><div class="activity-root"></div></div></div><div class="status-message" style="display: none;"></div></div><div id="google_translate_element"><div class="skiptranslate goog-te-gadget" dir="ltr" style="display: none;"><div id=":0.targetLanguage" style="white-space: nowrap;" class="goog-te-gadget-simple"><span id="lb" src="" class="goog-te-gadget-icon" alt="" style="background-image: url(&quot;https://translate.googleapis.com/translate_static/img/te_ctrl3.gif&quot;); background-position: -65px 0px;"></span><span style="vertical-align: middle;"><a aria-haspopup="true" class="goog-te-menu-value" href="javascript:void(0)"><span id="lb" src="" alt="" width="1" height="1"></span><span id="lb" src="" alt="" width="1" height="1"></span><span></span><span style="border-left: 1px solid rgb(187, 187, 187);">​</span><span style="color: rgb(118, 118, 118);" aria-hidden="true">▼</span></a></span></div></div></div><div class="goog-te-spinner-pos"><div class="goog-te-spinner-animation"><svg xmlns="http://www.w3.org/2000/svg" class="goog-te-spinner" width="96px" height="96px" viewBox="0 0 66 66"><circle class="goog-te-spinner-path" fill="none" stroke-width="6" stroke-linecap="round" cx="33" cy="33" r="30"></circle></svg></div></div><iframe class="goog-te-menu-frame skiptranslate" title="语言翻译微件" style="visibility: visible; display: none;" frameborder="0"></iframe><div class="notranslate recoverPage">原</div></body><div></div><iframe sandbox="allow-scripts" style="display: none;" srcdoc="&lt;!DOCTYPE html&gt;&lt;body&gt;&lt;script nonce=&quot;&quot;&gt;(function(){/*

 Copyright The Closure Library Authors.
 SPDX-License-Identifier: Apache-2.0
*/
var d=&quot;function&quot;==typeof Object.create?Object.create:function(a){var c=function(){};c.prototype=a;return new c},e;if(&quot;function&quot;==typeof Object.setPrototypeOf)e=Object.setPrototypeOf;else{var f;a:{var g={a:!0},l={};try{l.__proto__=g;f=l.a;break a}catch(a){}f=!1}e=f?function(a,c){a.__proto__=c;if(a.__proto__!==c)throw new TypeError(a+&quot; is not extensible&quot;);return a}:null}var m=e;/*

 SPDX-License-Identifier: Apache-2.0
*/
var n={};function p(){var a=&quot;undefined&quot;!==typeof window?window.trustedTypes:void 0;return null!==a&amp;&amp;void 0!==a?a:null}var q;function r(){var a,c;if(void 0===q)try{q=null!==(c=null===(a=p())||void 0===a?void 0:a.createPolicy(&quot;google#safe&quot;,{createHTML:function(b){return b},createScript:function(b){return b},createScriptURL:function(b){return b}}))&amp;&amp;void 0!==c?c:null}catch(b){q=null}return q};var t=function(a,c){if(c!==n)throw Error(&quot;Bad secret&quot;);this.g=a},u=function(){};t.prototype=d(u.prototype);t.prototype.constructor=t;if(m)m(t,u);else for(var v in u)if(&quot;prototype&quot;!=v)if(Object.defineProperties){var w=Object.getOwnPropertyDescriptor(u,v);w&amp;&amp;Object.defineProperty(t,v,w)}else t[v]=u[v];t.prototype.toString=function(){return this.g.toString()};function x(a){var c,b=null===(c=r())||void 0===c?void 0:c.createScriptURL(a);return new t(null!==b&amp;&amp;void 0!==b?b:a,n)};if(!function(){if(self.origin)return&quot;null&quot;===self.origin;if(&quot;&quot;!==location.host)return!1;try{return window.parent.escape(&quot;&quot;),!1}catch(a){return!0}}())throw Error(&quot;sandboxing error&quot;);
window.addEventListener(&quot;message&quot;,function(a){var c=a.ports[0];a=a.data;var b=a.callbackName.split(&quot;.&quot;),h=window;&quot;window&quot;===b[0]&amp;&amp;b.unshift();for(var k=0;k&lt;b.length-1;k++)h[b[k]]={},h=h[b[k]];h[b[b.length-1]]=function(y){c.postMessage(JSON.stringify(y))};b=document.createElement(&quot;script&quot;);a=x(a.url);if(a instanceof t)a=a.g;else throw Error(&quot;Unexpected type when unwrapping TrustedResourceUrl&quot;);b.src=a;document.body.appendChild(b)},!0);}).call(this);
&lt;/script&gt;&lt;/body&gt;" src="data:text/html;charset=UTF-8;base64,PCFET0NUWVBFIGh0bWw+PGJvZHk+PHNjcmlwdCBub25jZT0iIj4oZnVuY3Rpb24oKXsvKgoKIENvcHlyaWdodCBUaGUgQ2xvc3VyZSBMaWJyYXJ5IEF1dGhvcnMuCiBTUERYLUxpY2Vuc2UtSWRlbnRpZmllcjogQXBhY2hlLTIuMAoqLwp2YXIgZD0iZnVuY3Rpb24iPT10eXBlb2YgT2JqZWN0LmNyZWF0ZT9PYmplY3QuY3JlYXRlOmZ1bmN0aW9uKGEpe3ZhciBjPWZ1bmN0aW9uKCl7fTtjLnByb3RvdHlwZT1hO3JldHVybiBuZXcgY30sZTtpZigiZnVuY3Rpb24iPT10eXBlb2YgT2JqZWN0LnNldFByb3RvdHlwZU9mKWU9T2JqZWN0LnNldFByb3RvdHlwZU9mO2Vsc2V7dmFyIGY7YTp7dmFyIGc9e2E6ITB9LGw9e307dHJ5e2wuX19wcm90b19fPWc7Zj1sLmE7YnJlYWsgYX1jYXRjaChhKXt9Zj0hMX1lPWY/ZnVuY3Rpb24oYSxjKXthLl9fcHJvdG9fXz1jO2lmKGEuX19wcm90b19fIT09Yyl0aHJvdyBuZXcgVHlwZUVycm9yKGErIiBpcyBub3QgZXh0ZW5zaWJsZSIpO3JldHVybiBhfTpudWxsfXZhciBtPWU7LyoKCiBTUERYLUxpY2Vuc2UtSWRlbnRpZmllcjogQXBhY2hlLTIuMAoqLwp2YXIgbj17fTtmdW5jdGlvbiBwKCl7dmFyIGE9InVuZGVmaW5lZCIhPT10eXBlb2Ygd2luZG93P3dpbmRvdy50cnVzdGVkVHlwZXM6dm9pZCAwO3JldHVybiBudWxsIT09YSYmdm9pZCAwIT09YT9hOm51bGx9dmFyIHE7ZnVuY3Rpb24gcigpe3ZhciBhLGM7aWYodm9pZCAwPT09cSl0cnl7cT1udWxsIT09KGM9bnVsbD09PShhPXAoKSl8fHZvaWQgMD09PWE/dm9pZCAwOmEuY3JlYXRlUG9saWN5KCJnb29nbGUjc2FmZSIse2NyZWF0ZUhUTUw6ZnVuY3Rpb24oYil7cmV0dXJuIGJ9LGNyZWF0ZVNjcmlwdDpmdW5jdGlvbihiKXtyZXR1cm4gYn0sY3JlYXRlU2NyaXB0VVJMOmZ1bmN0aW9uKGIpe3JldHVybiBifX0pKSYmdm9pZCAwIT09Yz9jOm51bGx9Y2F0Y2goYil7cT1udWxsfXJldHVybiBxfTt2YXIgdD1mdW5jdGlvbihhLGMpe2lmKGMhPT1uKXRocm93IEVycm9yKCJCYWQgc2VjcmV0Iik7dGhpcy5nPWF9LHU9ZnVuY3Rpb24oKXt9O3QucHJvdG90eXBlPWQodS5wcm90b3R5cGUpO3QucHJvdG90eXBlLmNvbnN0cnVjdG9yPXQ7aWYobSltKHQsdSk7ZWxzZSBmb3IodmFyIHYgaW4gdSlpZigicHJvdG90eXBlIiE9dilpZihPYmplY3QuZGVmaW5lUHJvcGVydGllcyl7dmFyIHc9T2JqZWN0LmdldE93blByb3BlcnR5RGVzY3JpcHRvcih1LHYpO3cmJk9iamVjdC5kZWZpbmVQcm9wZXJ0eSh0LHYsdyl9ZWxzZSB0W3ZdPXVbdl07dC5wcm90b3R5cGUudG9TdHJpbmc9ZnVuY3Rpb24oKXtyZXR1cm4gdGhpcy5nLnRvU3RyaW5nKCl9O2Z1bmN0aW9uIHgoYSl7dmFyIGMsYj1udWxsPT09KGM9cigpKXx8dm9pZCAwPT09Yz92b2lkIDA6Yy5jcmVhdGVTY3JpcHRVUkwoYSk7cmV0dXJuIG5ldyB0KG51bGwhPT1iJiZ2b2lkIDAhPT1iP2I6YSxuKX07aWYoIWZ1bmN0aW9uKCl7aWYoc2VsZi5vcmlnaW4pcmV0dXJuIm51bGwiPT09c2VsZi5vcmlnaW47aWYoIiIhPT1sb2NhdGlvbi5ob3N0KXJldHVybiExO3RyeXtyZXR1cm4gd2luZG93LnBhcmVudC5lc2NhcGUoIiIpLCExfWNhdGNoKGEpe3JldHVybiEwfX0oKSl0aHJvdyBFcnJvcigic2FuZGJveGluZyBlcnJvciIpOwp3aW5kb3cuYWRkRXZlbnRMaXN0ZW5lcigibWVzc2FnZSIsZnVuY3Rpb24oYSl7dmFyIGM9YS5wb3J0c1swXTthPWEuZGF0YTt2YXIgYj1hLmNhbGxiYWNrTmFtZS5zcGxpdCgiLiIpLGg9d2luZG93OyJ3aW5kb3ciPT09YlswXSYmYi51bnNoaWZ0KCk7Zm9yKHZhciBrPTA7azxiLmxlbmd0aC0xO2srKyloW2Jba11dPXt9LGg9aFtiW2tdXTtoW2JbYi5sZW5ndGgtMV1dPWZ1bmN0aW9uKHkpe2MucG9zdE1lc3NhZ2UoSlNPTi5zdHJpbmdpZnkoeSkpfTtiPWRvY3VtZW50LmNyZWF0ZUVsZW1lbnQoInNjcmlwdCIpO2E9eChhLnVybCk7aWYoYSBpbnN0YW5jZW9mIHQpYT1hLmc7ZWxzZSB0aHJvdyBFcnJvcigiVW5leHBlY3RlZCB0eXBlIHdoZW4gdW53cmFwcGluZyBUcnVzdGVkUmVzb3VyY2VVcmwiKTtiLnNyYz1hO2RvY3VtZW50LmJvZHkuYXBwZW5kQ2hpbGQoYil9LCEwKTt9KS5jYWxsKHRoaXMpOwo8L3NjcmlwdD48L2JvZHk+"></iframe></html>